我正在用CURL設計一個簡單的twitter登錄腳本,當我嘗試執行它時,登錄表單將作爲GET發送,當我將它作爲POST發送時。由於這個,只有用戶主頁的標題顯示在瀏覽器中。Curl:POSTs轉換爲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? – SIFE 2011-04-11 20:25:42
我使用fiddler和LiveHTTPHeader插件爲Firefox進行了檢查。兩者都顯示爲GET請求。 – Dew 2011-04-11 20:47:50
嘗試刪除TRUE並將其替換爲1. – SIFE 2011-04-11 21:39:00