开发者

Curl: POSTs converting to GETs

I am designing a simple twitter login script with CURL and when I try to execute it, the login form is sent as GET when I am sending it as POST. Due to this only the header of the user's homepage is displayed in the browser.

Any input is appreciated.

<?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_COOKI开发者_Go百科EJAR, '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');

?>


I think the problem is this:

curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);

When redirecting through a Location header, the POST will be dropped and the request will be turned into a GET. I'm not sure if it's really necessary here or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜