时间:2021-07-01 10:21:17 帮助过:28人阅读
/**
* @author 追逐__something
* @version $id
*/
define('SCRIPT_ROOT',dirname(__FILE__).'/');
$act = trim(isset($_REQUEST['act']) ? $_REQUEST['act'] : "");
switch($act)
{
case 'login':
// 获取验证码
$code = trim($_REQUEST['code']);
// $loginParams为curl模拟登录时post的参数
// $loginParams['act'] = 'login';
$loginParams['password'] = '123123';
$loginParams['email'] = [email protected]';
$loginParams['captcha'] = $code;
// $cookieFile 为加载验证码时保存的cookie文件名
$cookieFile = SCRIPT_ROOT.'cookie.tmp';
// $targetUrl curl 提交的目标地址
$targetUrl = 'http://www.zhihu.com/login';
// 参数重置
$content = curlLogin($targetUrl, $cookieFile, $loginParams);
$targetUrl = "http://www.zhihu.com";
echo vget($targetUrl);
break;
case 'authcode':
// Content-Type 验证码的图片类型
header('Content-Type:image/png');
showAuthcode('http://www.zhihu.com/captcha.gif');
exit;
break;
}
/**
* 模拟登录
* @param string $url 提交到的地址
* @param string $cookieFile 保存cookie的文件
* @param string $loginParams 提交时要post的参数
* @return string $content 返回的内容
*/
function curlLogin($url, $cookieFile, $loginParams)
{
$header = array(
'Accept:*/*',
'Accept-Encoding:gzip,deflate,sdch',
'Accept-Language:zh-CN,zh;q=0.8,zh-TW;q=0.6',
'Cache-Control:no-cache',
'Connection:keep-alive',
'Content-Type:application/x-www-form-urlencoded; charset=UTF-8',
'Host:www.zhihu.com',
'Origin:http://www.zhihu.com',
'Referer:http://www.zhihu.com',
'X-Requested-With:XMLHttpRequest'
);
$curl = curl_init(); //启动一个curl会话
curl_setopt($curl, CURLOPT_URL, $url); //要访问的地址
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //设置HTTP头字段的数组
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); //从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.22 Safari/537.36'); //模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); //自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); //发送一个常规的Post请求
curl_setopt($curl,CURLOPT_POSTFIELDS, $loginParams); //提交查询信息
curl_setopt($curl,CURLOPT_COOKIEFILE, $cookieFile); //同时发送Cookie
curl_setopt($curl, CURLOPT_TIMEOUT, 30); //设置超时限制防止死循环
$result = curl_exec($curl); //执行一个curl会话
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200') {
$headSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
}
$this->_cookies($result);
curl_close($curl);