用curl登录twitter

Jus*_*00b 6 php twitter curl

我正在尝试使用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, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_COOKIESESSION, true);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $_CKS);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $_CKS);
            curl_setopt($ch, 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");

?>
Run Code Online (Sandbox Code Playgroud)

$ sPost看起来像这样:

会话[username_or_email] =用户会话[口令] =密码&remember_me = 1&scribe_log =&redirect_after_login =&authenticity_token = 6e706165609354bd7a92a99cf94e09140ea86c6f

代码首先获取登录表单字段,以便post变量具有正确的值(auth令牌).我只是尝试了一切,是的,我知道这有一个api但我宁愿找到如何为学习而做手册.

提前致谢.

g00*_*0ze 9

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, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($ch, CURLOPT_REFERER, "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, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));

# display server response
curl_exec($ch);
curl_close($ch);

?>
Run Code Online (Sandbox Code Playgroud)

PS:很抱歉没有第一次正确阅读你的帖子.