2016-09-27 79 views
0

Helloo,所有我想打電話從文件中的Web服務的Windows Server 2008的file_get_contents未能打開流

我已經連接到服務器,並安裝有XAMPP並放置所有必需的文件上。

這是我的代碼來調用web服務。

$result = file_get_contents("http://*******:8055/API.ashx?Method=Departure"); 
    $json = json_decode($result, true); 

    $departure_count = count($json['Response']); 

這使我對本地主機,但不能在服務器上正確的響應。我已經使用Google,他們告訴我應該使用cURL而不是file_gets_contents

然後我用這個代碼:

$url = 'http://*******:8055/API.ashx?Method=Departure'; 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HEADER, false); 
$data = curl_exec($curl); 
curl_close($curl); 
$json = json_decode($data, true); 
$departure_count = count($json['Response']); 

,它也使我在本地主機響應,但不在服務器上, 訪問的URL地址是:http://221.120.222.68:8080/wordpress/fare/

當我試圖打開$在瀏覽器中的URL,它給我的迴應

+0

你確定你的服務器配置允許傳出連接? –

+0

不,我不知道這 –

+0

@VladimirCvetic當我試圖在瀏覽器中打開$ url時,它給我的迴應 –

回答

0

由於錯誤消息說,所請求的流(URL)無法打開。

有許多可能的原因:

1. base URL is bad. 
2. username and/or password are bad 
3. username/password do not have permission on the server 
4. Your system cannot reach the server (firewall, PHP permissions) 

我會用以下策略進行調試:

1. Dump $url and write it down. 
2. Use a browser with debug tools (eg Firefox/Firebug) and try to access that URL. 
3. Look at the headers returned to see what error the server reports (if any). 
4. Think about why that error is returned... 
0

我用Google搜索,他們告訴我,我應該使用捲曲,而不是的file_gets_contents。

他們是誰?當然使用捲曲可以更容易地診斷問題 - 但它不會透露所有潛在的問題。

雖然從RAX答案有一些很好的提示,你說的那個代碼工作在不同的機器上 - 所以這個問題是關於服務器連接到服務。原因有很多,這可能是一個問題:

  • 所有傳出連接可以通過設計來阻擋
  • 有可能是與外部網絡的路由
  • 有可能是沒有可用的DNS服務

您應該與之交談的第一個人是支持服務器的人。同時,您可以嘗試部署一個簡單的腳本來嘗試解析主機名,並嘗試使用HTTP(例如這一個)從已建立的站點檢索內容。

例如

<?php 

$ip=gethostbyname('*******'); // the hostname you are trying to connect to 
print "IP = " . var_export($ip, true) . "<br />"; 
$content=file_get_contents("http://stackoverflow.com"); 
if (false===$content) { 
    print "failed to retrieve content - " 
     . var_dump($http_response_header, true); 
} else { 
    print "Successfully retrieved " . strlen($content) 
     . " bytes from http://stackoverflow.com"; 
} 
相關問題