>  기사  >  백엔드 개발  >  curl签到代码不工作

curl签到代码不工作

WBOY
WBOY원래의
2016-06-06 20:26:201134검색

老婆用的一个网站有签到功能, 签到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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.