php – 用curl登录twitter
发布时间:2020-12-13 14:13:48 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试使用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代码是: ?php $ch = curl_init
我正在尝试使用php curl登录twitter,但我认为我的请求有问题,因为我将此作为回复:
我正在使用的PHP代码是: <?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看起来像这样:
代码首先获取登录表单字段,以便post变量具有正确的值(auth令牌).我只是尝试了一切,是的,我知道这有一个api但我宁愿找到如何为学习而做手册. 提前致谢.
CURLOPT_COOKIESESSION用于指示新会话.这不是你想要的,因为你需要在第二篇文章中发送会话cookie.
我获得了Twitter登录以使用此代码: <?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:很抱歉没有第一次正确阅读你的帖子. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |