那麼,我想在某些頁面上使用cURL登錄。 當我在瀏覽器中將該數據粘貼到該地址欄中時 http://mywebpage.net/login.php?username=HERESMYUSERNAME&password=dded0102f44e7e0809520eb93055cb16 頁面將我帶到地址http://mywebpage.net/user.php,一切正常。 現在我想通過使用cURL來獲得相同的效果,但是不起作用。cURL不起作用
$url="http://mywebpage.net/login.php";
$cookie="cookie.txt";
$postdata = "username=cow&password=ass";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
if (!$result) {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); // make sure we closeany current curl sessions
die($http_code.' Unable to connect to server. Please come back later.');
}
echo $result;
curl_close($ch);
它向我顯示空白輸入的登錄頁面,它不會登錄我並移動到正確的地址。 當我鍵入www.mywebpage.net/user.php顯示 「錯誤401」
在這裏工作代碼:
$url="http://mywebpage.net/login.php?username=user&password=ddad0102f44e7f0800354eb11155cb16";
$cookie="cookie.txt";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
if (!$result) {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch); // make sure we closeany current curl sessions
die($http_code.' Unable to connect to server. Please come back later.');
}
echo $result;
curl_close($ch);
如何我現在可以重定向自己/all.php?
在第一個例子,你用'GET'方法發送數據和它的作品。在你的代碼上試試它也改爲'curl_setopt($ ch,CURLOPT_POST,0);'以GET方式發送 – safarov 2012-03-19 07:32:30
你將'CURLOPT_FOLLOWLOCATION'設置爲0,所以你明確地指示curl * not *移動到正確的地址。如果代碼是從其他地方複製粘貼的東西,我建議您逐行檢查它,並檢查每個選項的含義。 – JJJ 2012-03-19 07:35:36