3
回答
php中curl登录天涯社区
华为云实践训练营,热门技术免费实践!>>>   

登录天涯社区的程序,总是登录不了,不知道是哪里出问题了

代码如下:

<?php

date_default_timezone_set('Asia/Chongqing');

//登录
$cookieFile = dirname(__FILE__).DIRECTORY_SEPARATOR.'cookie_tianya_2.txt';
$loginUrl = 'http://passport.tianya.cn/login'; //登录地址
$postFields = array(
		'fowardURL' => 'http://focus.tianya.cn/',	
		'returnURL'	=> 'http://focus.tianya.cn/',
		'vpassword'	=> 'xxxxx',
		'vwriter'	=> 'xxxxx',
		'mflag'		=> 1 ,
	);
$ch = curl_init($loginUrl);
curl_setopt($ch , CURLOPT_HEADER, 0);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch , CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
curl_setopt($ch , CURLOPT_AUTOREFERER, 1);
curl_setopt($ch , CURLOPT_POST , 1);
curl_setopt($ch , CURLOPT_POSTFIELDS , http_build_query($postFields) );
curl_setopt($ch , CURLOPT_TIMEOUT , 30);
$html = curl_exec($ch);
curl_errno($ch) && die('<h2>Errno'.curl_error($ch).' LINE: '.__LINE__.'</h2>');

//正则匹配js写入的cookie和url验证
$patternCookie = '/document.cookie=\'(.*)\'/';
$patternUrl = '/<script type="text\/javascript" src="(.*)"><\/script>/';
preg_match_all($patternCookie , $html , $cookieArr);
preg_match_all($patternUrl , $html , $urlArr);
//处理cookie
$cookieResult = array(); //保存最终提取到的cookie,后面使用
foreach($cookieArr[1] AS $k=>$v){
	$tmpArr = array();
	$GMT = (micrTime()+2592000000)/1000;//cookie过期时间
	$GMT_stamp = strtotime( gmdate('D, d M Y H:i:s' , $GMT) );//GMT时间戳
	$arr = explode(';' , $v);//分号分隔的各个cookie
	// 检查cookie name
	$pos = mb_strpos($arr[0] , '=' , 0 , 'utf-8');
	$ckn = mb_substr($arr[0] , 0 , $pos , 'utf-8');//截取的cookie名称
	$tmpArr['name'] = $ckn;
	//$tmpVal = mb_substr($arr[0] , $pos+1 , -1 , 'utf-8');//fuck
	$tmpVal = mb_substr($arr[0] , $pos+1);
	$tmpTime = time();//秒数
	$tmpArr['value'] = str_replace(array('\'+parseInt(new Date().getTime()/1000)+\'' , '\'+(parseInt(new Date().getTime()/1000)+2592000)+\'') , array($tmpTime , $tmpTime+2592000) , $tmpVal);
	$tmpArr['expires'] = $GMT_stamp;
	$tmpArr['path'] = '/';
	$tmpArr['domain'] = 'tianya.cn';
	$cookieResult[] = $tmpArr;
}

//更新提取到的cookci
updateCookie($cookieResult);

/*--访问提取到的url地址--*/
foreach($urlArr[1] AS $k=>$v){
	accessUrl($v);
}

//转向登陆后的页面
$successUrl = 'http://focus.tianya.cn/';
$ch = curl_init($successUrl);
curl_setopt($ch , CURLOPT_HEADER, 0);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch , CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
curl_setopt($ch , CURLOPT_AUTOREFERER, 1);
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true);
curl_setopt($ch , CURLOPT_COOKIEJAR , $cookieFile);
curl_setopt($ch , CURLOPT_COOKIEFILE , $cookieFile);
curl_setopt($ch , CURLOPT_TIMEOUT , 30);
$html = curl_exec($ch);
curl_errno($ch) && die('<h2>Errno'.curl_error($ch).' LINE: '.__LINE__.'</h2>');

echo $html;











///////////////////////////////////////////////////////////////////////////////////////////////////
//访问一个url地址
function accessUrl($url){
	global $cookieFile;
	$ch = curl_init($url);
	curl_setopt($ch , CURLOPT_HEADER, 0);
	curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch , CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
	curl_setopt($ch , CURLOPT_AUTOREFERER, 1);
	curl_setopt($ch , CURLOPT_COOKIEJAR , $cookieFile);
	curl_setopt($ch , CURLOPT_COOKIEFILE , $cookieFile);
	curl_setopt($ch , CURLOPT_TIMEOUT , 30);
	$html = curl_exec($ch);
	curl_errno($ch) && die('<h2>Errno'.curl_error($ch).' LINE: '.__LINE__.'</h2>'); 
}


//更新cookie
function updateCookie($cokArr=array()){
	global $cookieFile;
	$cokStr = '';
	foreach($cokArr AS $k=>$v){
		$cokStr .= $v['domain']."\t".'TRUE'."\t".$v['path']."\t".'FALSE'."\t".$v['expires']."\t".$v['name']."\t".$v['value'].PHP_EOL;
	}
	//file_put_contents($cookieFile , $cokStr);
	if(is_file($cookieFile) && (filesize($cookieFile) > 0)){
		file_put_contents($cookieFile , $cokStr);
	}else{
		file_put_contents($cookieFile , $cokStr , FILE_APPEND);
	}
	return $cokStr;
}


//返回毫秒
function micrTime(){
	return round(microtime(true)*1000);
}

//方便查看数据
function pre($val){
	echo '<pre style="padding:3px;background-color:#EEE;border:1px solid green;">';
	print_r($val);echo '</pre>';
}
?>


谢谢

举报
voov
发帖于6年前 3回/1K+阅
顶部