Helloo,我试图从Windows Server 2008上的文件中调用Web服务。
我已连接到服务器并在那里安装了xampp并放置了所有必需的文件。
这是我调用网络服务的代码。
$result = file_get_contents("http://*******:8055/API.ashx?Method=Departure");
$json = json_decode($result, true);
$departure_count = count($json['Response']);
它在localhost上给出了正确的响应,但在服务器上却没有。我用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']);
它也给了我对localhost的响应,但没有在服务器上, 访问URl的地址是:http://221.120.222.68:8080/wordpress/fare/
当我尝试在浏览器中打开$ url时,它会给我回复
答案 0 :(得分: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...
答案 1 :(得分:0)
我用Google搜索,他们告诉我应该使用cURL而不是file_gets_contents。
他们是谁?当然使用curl 应该可以更容易地诊断问题 - 但它不会揭示所有潜在的问题。
虽然Rax的答案有一些很好的提示,但是你说代码可以在不同的机器上运行 - 所以问题在于服务器如何连接到服务。造成这个问题的原因有很多:
您应该与之交谈的第一个人是支持/配置服务器的人。同时,您可以尝试部署一个简单的脚本来尝试解析主机名,并尝试使用HTTP从一个完善的站点检索内容(例如,这个)。
e.g。
<?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";
}