>  기사  >  백엔드 개발  >  php curl 模拟登陆京东,”总提示刷新页面后重新提交“

php curl 模拟登陆京东,”总提示刷新页面后重新提交“

WBOY
WBOY원래의
2016-06-23 13:37:161500검색

开发完事之后,登陆是可以的,但是后来一直提示
Array
(
    [username] => 请刷新页面后重新提交
)
这个不知道是怎么回事,一直没找到什么原因,郁闷
话不多说,上代码,盼望做过的英雄帮忙下,

<?phpclass util_curl {	var $headers;	var $user_agent;	var $compression;	var $cookie_file;	var $proxy;	function util_curl($cookies=TRUE,$cookie='cookies.txt',$header = array(),$compression='gzip',$proxy='') {		if(empty($header))		{			$this->headers[] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';			$this->headers[] = 'Connection: Keep-Alive';			//$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';			//$this->headers[] = '';		}		$this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)';		$this->compression=$compression;		$this->proxy=$proxy;		$this->cookies=$cookies;		if ($this->cookies == TRUE) $this->cookie($cookie);	}	function cookie($cookie_file) {		if (file_exists($cookie_file)) {			$this->cookie_file=$cookie_file;		} else {			if($h = fopen($cookie_file,'w')){				$this->cookie_file=$cookie_file;				fclose($h);			} else {				$this->error('The cookie file could not be opened. Make sure this directory has the correct permissions');			}		}	}	function get($url) {		$process = curl_init($url);		curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);		curl_setopt($process, CURLOPT_HEADER, 0);		curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);		if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);		if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);		curl_setopt($process,CURLOPT_ENCODING , $this->compression);		curl_setopt($process, CURLOPT_TIMEOUT, 30);		if ($this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy);		curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);		curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);		$return = curl_exec($process);		curl_close($process);		return $return;	}	function post($url,$data) {		$process = curl_init($url);		curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);		curl_setopt($process, CURLOPT_HEADER, 0);		curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);		if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);		if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);		curl_setopt($process, CURLOPT_ENCODING , $this->compression);		curl_setopt($process, CURLOPT_TIMEOUT, 30);		if ($this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy);		curl_setopt($process, CURLOPT_POSTFIELDS, $data);		curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);		curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);		curl_setopt($process, CURLOPT_POST, 1);		$return = curl_exec($process);		curl_close($process);		return $return;	}	function vpost($url,$data){ // 模拟提交数据函数		$curl = curl_init(); // 启动一个CURL会话		//$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=GBK';		curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);		curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在		curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent); // 模拟用户使用的浏览器		//curl_setopt($curl, CURLOPT_NOBODY, 0);		if ($this->cookies == TRUE) curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file);;		if ($this->cookies == TRUE) curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie_file);		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, $data); // Post提交的数据包		curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环		curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回		$tmpInfo = curl_exec($curl); // 执行操作		if (curl_errno($curl)) {			echo 'Errno'.curl_error($curl);//捕抓异常		}		curl_close($curl); // 关闭CURL会话		return $tmpInfo; // 返回数据	}	function vget($url){ // 模拟提交数据函数		$curl = curl_init(); // 启动一个CURL会话		//$this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=GBK';		curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);		curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在		curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent); // 模拟用户使用的浏览器		//curl_setopt($curl, CURLOPT_NOBODY, 0);		if ($this->cookies == TRUE) curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file);;		if ($this->cookies == TRUE) curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie_file);		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转		curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer		//curl_setopt($curl, CURLOPT_POST, 0); // 发送一个常规的Post请求		//curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包		curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环		curl_setopt($curl, CURLOPT_HEADER, 1); // 显示返回的Header区域内容		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回		$tmpInfo = curl_exec($curl); // 执行操作		if (curl_errno($curl)) {			echo 'Errno'.curl_error($curl);//捕抓异常		}		curl_close($curl); // 关闭CURL会话		return $tmpInfo; // 返回数据	}	function error($error) {		echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>util_curl Error</b><br>$error</div></center>";		die;	}}$shopUser = array();$shopUser['name'] = 'xxxx';$shopUser['password'] = 'xxxxxxxxx';$cookieFile = './cookie_aaa.txt';$http = new util_curl(true,$cookieFile);//1.获取登录页面代码,从中找出uuid变量值$showLoginUrl = "https://passport.jd.com/new/login.aspx";$loginViewStr = $http->vget($showLoginUrl);preg_match('/<input type="hidden" id="uuid" name="uuid" value="([\s\S]*?)"/',$loginViewStr,$rs);if(!isset($rs[1])) throw new Exception('分析页面源码分析uuid失败');$uuid = $rs[1];$loginUrl = "https://passport.jd.com/uc/loginService?uuid={$uuid}&ReturnUrl=http%3A%2F%2Fwww.jd.com%2F";$fields = "uuid={$uuid}&loginname={$shopUser['name']}&nloginpwd={$shopUser['password']}&loginpwd={$shopUser['password']}&machineNet=&machineCpu=&machineDisk=&authcode=";$response = $http->vpost($loginUrl,$fields);$jsonObj = json_decode(str_replace(array('(',')'),'',$response),true);if(isset($jsonObj['success'])) echo '登陆成功';print_r($jsonObj);exit;


回复讨论(解决方案)

没有做过这个的人吗

需要输入验证码吧

我是用易语言写,但方法基本一样,也是提示“请刷新页面后重新提交”,我看抓的提交数据包是:
“uuid=”+uuid+“&loginname="+用户名+&nloginpwd="+密码+"&loginpwd="+密码+"&machineNet=&machineCpu=&machineDisk=&authcode=&mJODPRgWFp=DKNDP”

主要是authcode=后面还有一个mJODPRgWFp=DKNDP的东西,应该它是有用处的,如果我们不能正确提交这个数据就会被判无效吧,唉,我也研究不出来。

我的Q 957997543

cookie 缓存文件名应使用绝对路径

但即便是这样做了,也不能保证成功
京东应该和淘宝一样,使用了 js 操控第三方验证服务。所以的的程序不能解析和执行 js 的话,成功的概率为 0

这个
http://blog.csdn.net/jdgdf566/article/details/13632111
恐怕也起不了作用。既然原因如4楼说的的话。

不知楼主解决了没有,我也遇到同样问题!

楼主解决了吗?同求解决方案

楼主问题解决了么?求解决方案

京东用了flashcookie,不好获取,我用c++ builder搞半天也没取出关键cookie:
track=c09a16fa-8dfd-015b-b289-1372d4c03405
只要得到这个cookie其他的就好办了。

楼主 解决的吗 求分享成果

看一下单点登陆的过程就知道淘宝和京东如何获取这些信息了。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.