我正在尝试使用PHP curl登录twitter,但我认为我的请求有问题,因为我将此作为回复:
Something is technically wrong.
Thanks for noticing—we\’re going to fix it up and have things back to
normal soon.
<?PHP $ch = curl_init($sTarget); curl_setopt($ch,CURLOPT_HEADER,false); curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_POSTFIELDS,$sPost); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,CURLOPT_RETURNTRANSFER,CURLOPT_SSL_VERIFYPEER,CURLOPT_SSL_VERIFYHOST,CURLOPT_COOKIESESSION,CURLOPT_COOKIEJAR,$_CKS); curl_setopt($ch,CURLOPT_COOKIEFILE,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']); curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-type: multipart/form-data")); curl_setopt($ch,CURLOPT_REFERER,"https://twitter.com"); ?>
$sPost看起来像这样:
session[username_or_email]=user&session[password]=password&remember_me=1&scribe_log=&redirect_after_login=&authenticity_token=6e706165609354bd7a92a99cf94e09140ea86c6f
代码首先获取登录表单字段,以便post变量具有正确的值(auth令牌).我只是尝试了一切,是的,我知道这有一个api但我宁愿找到如何为学习而做手册.
提前致谢.
CURLOPT_COOKIESESSION用于指示新会话.这不是你想要的,因为你需要在第二篇文章中发送会话cookie.
原文链接:https://www.f2er.com/php/130620.html<?PHP # First call gets hidden form field authenticity_token # and session cookie $ch = curl_init(); $sTarget = "https://twitter.com/"; curl_setopt($ch,CURLOPT_URL,$sTarget); curl_setopt($ch,true); curl_setopt($ch,false); curl_setopt($ch,$_SERVER['HTTP_USER_AGENT']); curl_setopt($ch,"/tmp/cookie.txt"); curl_setopt($ch,"https://twitter.com/"); $html = curl_exec($ch); # parse authenticity_token out of html response preg_match('/<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token"\/>/',$html,$match); $authenticity_token = $match[1]; $username = "your@email.com"; $password = "password"; # set post data $sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token"; # second call is a post and performs login $sTarget = "https://twitter.com/sessions"; curl_setopt($ch,$sPost); curl_setopt($ch,array("Content-type: application/x-www-form-urlencoded")); # display server response curl_exec($ch); curl_close($ch); ?>
PS:很抱歉没有第一次正确阅读你的帖子.