2012-12-04 36 views
1

我正在試圖讓curl輸出。這是在該地址輸出的基本文本字符串。Curl返回特定端口上的空白頁面

我做這個代碼,它不工作,但工作正常,如果我拿出27017和加載主域:

$cookie_file_path = "cookiejar.txt"; 
$fp = fopen("$cookie_file_path","w"); 
fclose($fp); 
$LOGINURL = "http://www.myserver.org:27017"; 
$agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)"; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$LOGINURL); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 
    $result = curl_exec ($ch); 
echo $result; 

任何想法,爲什麼它不工作?任何幫助將不勝感激。不確定它是否相關,但Web服務器與在27017端口上輸出的應用程序在同一臺機器上運行。

更新:

$cookie_file_path = "cookiejar.txt"; 
$username = "myusername"; 
$password = "mypassword"; 
$LOGINURL = "http://www.myserver.org"; 
$agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)"; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$LOGINURL); 
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
    curl_setopt($ch, CURLOPT_PORT , "27017"); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 
    $result = curl_exec ($ch); 

echo $result; 

更新2:

解決。

原來,它需要對此特定實例進行摘要認證。

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); 

解決問題...

感謝Barmar讓我意識到這確實需要一個用戶名/密碼。

回答

0

當我點擊鏈接時,我會被要求輸入用戶名和密碼。您需要使用CURLOPT_USERPWD選項來提供這樣的:

$username = 'XXX'; 
$password = 'YYY'; 

curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 

這裏的結果我得到的,當我做:

$ curl -s -D/dev/stdout 'http://www.nationalgaming.org:27017' 
HTTP/1.1 401 Unauthorized 
Content-Length: 0 
WWW-Authenticate: Digest qop="auth", realm="www.nationalgaming.org", nonce="1354591712" 

這表明該網頁需要登錄。

+0

那麼,如果你問索引index.html它會要求輸入密碼(帶你到不同的頁面來管理服務器)。但是,如果您直接訪問它而不指定index.html部分(僅限於根域),則不應要求輸入密碼。至少它在使用chrome或ie9時從未問過我。或者,也許Chrome/ie9自動指定某種默認的訪客用戶名和密碼。 – TechnIckS

+0

我只是點擊了你放在你的問題中的鏈接,它並沒有說'index.html'。瀏覽器可選擇記住登錄信息,所以你可能已經保存在瀏覽器中。 – Barmar

+0

不是這樣。我厭倦了不同的機器,關閉了瀏覽器,清除了所有的cookies /歷史記錄等,重新啓動的機器等等。它只在訪問/index.html時要求用戶/通行證。看到這個[鏈接](http://www.unknownworlds.com/ns2/wiki/index.php/Dedicated_Server#Web_API) – TechnIckS