cURL不起作用

时间:2012-03-19 07:27:48

标签: php curl

好吧,我想在某个页面上使用cURL登录。 当我在浏览器中以该格式粘贴地址栏中的数据时 http://mywebpage.net/login.php?username=HERESMYUSERNAME&password=dded0102f44e7e0809520eb93055cb16 页面将我带到地址http://mywebpage.net/user.php,一切正常。 现在我希望通过使用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显示“Err 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?

1 个答案:

答案 0 :(得分:1)

我看到3件事错了

  1. 您使用的是CURL POST而不是GET
  2. CURLOPT_FOLLOWLOCATION设置为false,但您的登录脚本会重定向到user.php,即使身份验证有效,您也会获得空响应。我认为你应该将CURLOPT_FOLLOWLOCATION设置为true
  3. 您提供的无效或无效的网址..我不确定这是真实网址还是仅仅是一个示例。
  4. 由于 :)