Home >Backend Development >PHP Tutorial >javascript - php如何模拟登录http://www.19lou.com/login, curl无法登陆成功
求大神给个php示例代码模拟登录http://www.19lou.com/login
我用curl试了无法登陆成功。
账号给大家注册好了
谢谢。
求大神给个php示例代码模拟登录http://www.19lou.com/login
我用curl试了无法登陆成功。
账号给大家注册好了
谢谢。
简单测试了一下,没有特殊的校验啊,传这些参数就好了,refererUrl这个参数可以从
这个链接的r=处找到
================分割线======================
贴代码吧
<code>function curl_post_contents($url, $params, $cookie_file, $has_cookie, $use_http_build_query = false, $headers = array(), $time_out = 30) { if ($use_http_build_query) { $params = http_build_query($params); } $curlHandle = curl_init(); if ($headers) { curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers); } curl_setopt($curlHandle, CURLOPT_POST, 1); curl_setopt($curlHandle, CURLOPT_URL, $url); curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, 0); //让CURL支持HTTPS访问 curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, $time_out); curl_setopt($curlHandle, CURLOPT_TIMEOUT, $time_out); curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $params); curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, 1);//跳转到重定向的地址 if ($has_cookie) curl_setopt($curlHandle, CURLOPT_COOKIEFILE, $cookie_file); else curl_setopt($curlHandle, CURLOPT_COOKIEJAR, $cookie_file); $result = curl_exec($curlHandle); curl_close($curlHandle); return $result; } $data = array( 'refererUrl'=>'aHR0cDovL3d3dy4xOWxvdS5jb20=', 'checked' => 0, 'userName' => 'wangtets123', 'userPass' => '#1234567890', 'ssl' => true ); $url = 'https://www.19lou.com/login'; $cookie_file = dirname(__FILE__)."/pic.cookie"; $result = curl_post_contents($url,$data,$cookie_file,false,true); echo $result;</code>
cookie放在pic.cookie里面,接下来要做什么把这个cookie放进去就好了
谢谢大家。已经解决
代码如下
<code><?php function login_post($url, $cookie, $post) { $curl = curl_init();//初始化curl模块 curl_setopt($curl, CURLOPT_URL, $url);//登录提交的地址 curl_setopt($curl, CURLOPT_HEADER, 0);//是否显示头信息 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);//是否自动显示返回的信息 curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //设置Cookie信息保存在指定的文件中 curl_setopt($curl, CURLOPT_POST, 1);//post方式提交 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_AUTOREFERER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息 curl_exec($curl);//执行cURL curl_close($curl);//关闭cURL资源,并且释放系统资源 } function get_content($url1, $cookie) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //读取cookie $rs = curl_exec($ch); //执行cURL抓取页面内容 curl_close($ch); return $rs; } $post = array ( 'refererUrl'=>'aHR0cDovL3d3dy4xOWxvdS5jb20v', 'checked'=>'0', 'userName'=>$user, 'userPass'=>$pass, 'captcha'=>'', 'remember'=>'1', 'ssl'=>'true', ); $url = "https://www.19lou.com/login"; $cookie = dirname(__FILE__) . '/sss.txt'; //登录后要获取信息的地址 $url1 = "http://www.19lou.com/wap/myinfo/index"; //模拟登录 login_post($url, $cookie, $post); //获取登录页的信息 echo $content = get_content($url1, $cookie); </code>
不知道php
的curl会不会在302重定向的时候,丢失cookie, 刚刚看了看,在login之后是302重定向了
在python的urllib2
遇到302时候,默认设置会丢失cookie,所以个人感觉如果你提交的参数没有问题的话, 可能是这个原因。
你想要的curl,bash版本:
<code>curl 'https://www.19lou.com/login' -H 'Pragma: no-cache' -H 'Origin: http://www.19lou.com' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: zh-CN,zh;q=0.8,ja;q=0.6,en;q=0.4' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Cache-Control: no-cache' -H 'Referer: http://www.19lou.com/login' -H 'Cookie: JSESSIONID=1F755C103A98DD613F80F3AFB99B3EC9; sys=67174666.20480.0000; _DM_SID_=b13ac27ac3d29a85ad6094b375981b8f; _DM_S_=487781a528838b00e091f3d500a33239' -H 'Connection: keep-alive' -H 'DNT: 1' --data 'refererUrl=aHR0cDovL3d3dy4xOWxvdS5jb20%3D&checked=0&userName=wangtets123&userPass=%231234567890&captcha=&remember=1&ssl=true' --compressed</code>
善用chrome的F12,很强大的。
逐步分析 就行
人家做了防跨站访问的话就不能实现了