<?php
require('phpQuery-master/phpQuery/phpQuery.php');
try {
//模拟登录表单Fields
$post = [
'username' => '100000',
'password' => '123456',
'submit' => '登录'
];
$login_url = "http://www.test.com/User/Auth/login";
\phpQuery::newDocumentFile($login_url);
//引入hash
$post['__hash__'] = pq('input[name="__hash__"]')->val();
$cookie = dirname(__FILE__) . '/cookie.conf';
$url = "http://www.test.com/User/Profile/index";
//调用模拟登录
login_post($login_url, $cookie, $post);
//登录后,调用get_content()函数获取登录后指定的页面信息
$content = get_content($url, $cookie);
var_dump($content);
} catch (\Exception $exception) {
var_dump($exception);
}
//file_put_contents('save.txt', $content); //保存抓取的页面内容
//模拟登录
function login_post($url, $cookie, $post)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
//301等重定向
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_exec($ch);
curl_close($ch);
}
//用cookie获取登陆后链接界面
function get_content($url, $cookie)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
//301等重定向
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$rs = curl_exec($ch);
curl_close($ch);
return $rs;
}