【发布时间】:2011-04-11 20:18:33
【问题描述】:
我正在使用 CURL 设计一个简单的 twitter 登录脚本,当我尝试执行它时,当我将登录表单作为 POST 发送时,它会作为 GET 发送。因此,浏览器中只会显示用户主页的标题。
感谢任何输入。
<?php
function tw_connect($user, $pwd) {
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16";
$ch = curl_init('http://www.twitter.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$c = curl_exec($ch);
curl_close($ch);
preg_match_all('#authenticity_token" type="hidden" value="(.*)"#U', $c, $g);
$g = $g[1][0];
$post = 'authenticity_token=' . trim($g[1]) . '&authenticity_token=' . trim($g[1]) .
'&return_to_ssl=false&redirect_after_login=&session' . urlencode("[username_or_email]") .
'=' . $user . '&' . urlencode("session[password]") . '=' . $pwd . '&commit=' . urlencode("Sign In");
$ch2 = curl_init('https://twitter.com/sessions');
curl_setopt($ch2, CURLOPT_USERAGENT, $agent);
curl_setopt($ch2, CURLOPT_REFERER, 'http://twitter.com/');
curl_setopt($ch2, CURLOPT_POST, TRUE);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch2, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch2, CURLOPT_COOKIESESSION, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
$v = curl_exec($ch2);
echo $v;
}
tw_connect('username','password');
?>
【问题讨论】:
-
是什么让您认为您的脚本将数据作为 GET 而不是 POST 发送?
-
我用 fiddler 和 LiveHTTPHeader 插件检查了它。两者都将其显示为 GET 请求。
-
尝试去掉TRUE,换成1。
-
当我将 TRUE 替换为 1 时,我收到此错误“未定义的偏移量:第 15 行的 C:\file\address.php 中的 1”