Curl:将post转换为get


Curl: POSTs converting to GETs

我正在设计一个简单的twitter与CURL登录脚本,当我尝试执行它时,登录表单作为GET发送,当我发送它作为POST。因此,浏览器中只显示用户主页的标题。

欢迎输入。

<?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');
?>

我认为问题是这样的:

curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);

当通过Location标头重定向时,POST将被丢弃,请求将转换为GET。我不确定这里是否真的有必要。