2010-11-25 130 views
0

我有一個URL包含所有信息在網址(用戶名/密碼/內容等)php curl - 通過瀏覽器訪問url響應:200通過curl響應訪問url:401?

如果我訪問我的瀏覽器中的網址,我得到了一個成功的迴應。

但是如果我通過捲曲訪問URL,我得到401

上有網址身份驗證。

這可能是什麼原因造成的?

代碼:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://mconnect.co.nz/v1/sendmessage?appname=app&pass=pass&msgclass=test&msgid=6&body=Some+Content&to=02712345678'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, $mconnect[$index]['app_name'] . ":" . $mconnect[$index]['app_pass']); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.12 (KHTML, like Gecko) Chrome/9.0.587.0 Safari/534.12'); 
curl_setopt($ch, CURLOPT_FAILONERROR, true); 
curl_setopt($ch, CURLOPT_HTTPGET, true); 
curl_setopt($ch, CURLOPT_POST, false); 
if(curl_exec($ch) === false) 
    echo 'fail: '.curl_error($ch); 


更新... ...奇怪

所以,

我建設我的網址通過 $ mconnect [$指數] ['網址']。 '?' 。 http_build_query($網址);

它給了我上面的網址。

所以,

如果我有

$url = $mconnect[$index]['url'] . '?' . http_build_query($url); 
curl_setopt($ch, CURLOPT_URL, $url); 

我得到的401

但是,

如果我再做 echo $mconnect[$index]['url'] . '?' . http_build_query($url);

我得到

http://mconnect.co.nz/v1/sendmessage?appname=app&pass=pass&msgclass=test&msgid=6&body=Some+Content&to=02712345678

所以,如果我再做

$url = 'http://mconnect.co.nz/v1/sendmessage?appname=app&pass=pass&msgclass=test&msgid=6&body=Some+Content&to=02712345678'; 
    curl_setopt($ch, CURLOPT_URL, $url); 

然後,它的工作原理...

什麼想法?

+0

你確定你正在做的HTTP發佈/獲取使用捲曲? – 2010-11-25 21:32:40

+0

剛剛添加的代碼 – Hailwood 2010-11-25 21:42:16

回答

0

雖然不知道爲什麼該修補程序是建立在URL自己的,而不是使用PHP 5的build_query_string

0

服務器可能正在驗證用戶代理。您可能需要將其設置爲任何瀏覽器使用的。

+0

剛剛添加的代碼,是的,我認爲這可能也是如此,但不是。 – Hailwood 2010-11-25 21:42:34

+0

這是一個非常好奇的用戶代理。嘗試擺脫額外的瀏覽器,他們可能會做一些有趣的事情,以防止這... – 2010-11-25 21:44:47

0

您發送的參數可能需要通過POST發送。

$posts = array('pass' => 'password', 'body' => 'lots of content'); // ... etc; 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $posts); 

如果是這種情況,您可能不需要使用CURLOPT_USERPWD選項。 「

0

」網址上沒有身份驗證。「但我看到

curl_setopt($ch, CURLOPT_USERPWD, $mconnect[$index]['app_name'] . ":" . $mconnect[$index]['app_pass']); 

是不是認證?

注: 你也可以嘗試發送這樣

curl_setopt($ch, CURLOPT_URL, 'http://'.$mconnect[$index]['app_name'] . ":" . $mconnect[$index].'@mconnect.co.nz/v........