Home > Article > Backend Development > curl签到代码不工作
老婆用的一个网站有签到功能, 签到10天送5块钱现金券. 老婆大人想要现金券,但是又不想每天去签到, 让我解决. 我尝试了很久也没有搞定,希望高手指点一下.
下面我的代码:
<code><?php $header = array( 'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36', 'Content-Type: application/x-www-form-urlencoded', ); $loginurl = 'http://cn.example.com/customer/account/login/'; $preg_once = '/<input\sname="form_key"\stype="hidden"\svalue="([a-zA-Z0-9_-]+)"/'; $session = curl_init($loginurl); curl_setopt($session, CURLOPT_HTTPHEADER, $header); curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); curl_setopt($session, CURLOPT_NOPROGRESS, false); curl_setopt($session, CURLOPT_COOKIE, 'cookie.txt'); $login = curl_exec($session); preg_match($preg_once, $login, $onceArr); $form_key = $onceArr[1]; //print $form_key; $data_to_post = array(); $data_to_post['form_key'] = $form_key; $data_to_post['login[username]']='13111111111@test.com'; $data_to_post['login[password]']='13111111111'; $data_to_post['send']=''; $post_string = http_build_query($data_to_post); //print $post_string; $posturl = 'http://cn.example.com/customer/account/loginPost/'; curl_setopt($session, CURLOPT_URL, $posturl); curl_setopt($session, CURLOPT_POST, 1); curl_setopt($session, CURLOPT_POSTFIELDS,$post_string); curl_setopt($session, CURLOPT_FOLLOWLOCATION, TRUE); $post = curl_exec($session); print $post; $signinurl = 'http://cn.example.com/signin/'; curl_setopt($session, CURLOPT_URL, $signinurl); curl_setopt($session, CURLOPT_HTTPGET, TRUE); $signin = curl_exec($session); //print $signin;</code></code>
里面的账号密码是新申请的账号,可以登录.
关键是curl函数的参数太多了,而且不太熟悉怎么调试curl的结果. 请高手指点为什么这段代码不工作.
老婆用的一个网站有签到功能, 签到10天送5块钱现金券. 老婆大人想要现金券,但是又不想每天去签到, 让我解决. 我尝试了很久也没有搞定,希望高手指点一下.
下面我的代码:
<code><?php $header = array( 'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36', 'Content-Type: application/x-www-form-urlencoded', ); $loginurl = 'http://cn.example.com/customer/account/login/'; $preg_once = '/<input\sname="form_key"\stype="hidden"\svalue="([a-zA-Z0-9_-]+)"/'; $session = curl_init($loginurl); curl_setopt($session, CURLOPT_HTTPHEADER, $header); curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); curl_setopt($session, CURLOPT_NOPROGRESS, false); curl_setopt($session, CURLOPT_COOKIE, 'cookie.txt'); $login = curl_exec($session); preg_match($preg_once, $login, $onceArr); $form_key = $onceArr[1]; //print $form_key; $data_to_post = array(); $data_to_post['form_key'] = $form_key; $data_to_post['login[username]']='13111111111@test.com'; $data_to_post['login[password]']='13111111111'; $data_to_post['send']=''; $post_string = http_build_query($data_to_post); //print $post_string; $posturl = 'http://cn.example.com/customer/account/loginPost/'; curl_setopt($session, CURLOPT_URL, $posturl); curl_setopt($session, CURLOPT_POST, 1); curl_setopt($session, CURLOPT_POSTFIELDS,$post_string); curl_setopt($session, CURLOPT_FOLLOWLOCATION, TRUE); $post = curl_exec($session); print $post; $signinurl = 'http://cn.example.com/signin/'; curl_setopt($session, CURLOPT_URL, $signinurl); curl_setopt($session, CURLOPT_HTTPGET, TRUE); $signin = curl_exec($session); //print $signin;</code></code>
里面的账号密码是新申请的账号,可以登录.
关键是curl函数的参数太多了,而且不太熟悉怎么调试curl的结果. 请高手指点为什么这段代码不工作.
用开发者工具抓包看看
两个请求为什么要共用一个curl句柄呢?
在 posturl之前重新初始化一个curl句柄
<code> $session = curl_init(); $posturl = 'http://cn.memebox.com/customer/account/loginPost/'; curl_setopt($session, CURLOPT_URL, $posturl);</code>
<code class="php">//调试curl if(!curl_errno($ch)) { $info = curl_getinfo($ch); }</code>