卷曲:POST转换为GET

时间:2011-04-11 20:18:34

标签: php post curl header get

我正在使用CURL设计一个简单的Twitter登录脚本,当我尝试执行它时,登录表单作为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');

?>

1 个答案:

答案 0 :(得分:1)

我认为问题是:

curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);

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