【发布时间】:2017-01-09 18:08:06
【问题描述】:
我将我的应用程序从我的开发环境 XAMPP 转移到我们旧的 Ubuntu Web 服务器 (Ubuntu 12.04.5 LTS)。在服务器上,安装了 PHP 5.3。 我启用了 curl,它正在工作。
当我尝试运行我的代码时,出现“找不到主机”错误。我已经尝试在this article 的帮助下调试 curl,但我无法弄清楚问题所在。
我尝试了以下代码:
function getTest($parameter, $username, $password){
$response = file_get_contents_curl('[rest url with parameter]', $username, $password);
print_r($response);
}
function file_get_contents_curl($url, $username, $password) {
ob_start();
$out =fopen('php://output', 'w');
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_STDERR, $out);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
$data = curl_exec($curl);
fclose($out);
$debug=ob_get_clean();
print_r($debug);
curl_close($curl);
上面的代码包含文章中的调试代码。以下是输出(除了包含错误“找不到主机”的 $response 输出):
GET [rest url]
HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)
Host: [host]
Accept: */*
Proxy-Connection: Keep-Alive
< HTTP/1.1 502 Host not found
< Date: Mon, 09 Jan 2017 17:49:42 GMT
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Type: text/html; charset="UTF-8"
< Content-Length: 2556
< Accept-Ranges: none
* HTTP/1.1 proxy connection set close!
< Proxy-Connection: close
< Connection: close
<
* Closing connection #0
有没有人知道,那里可能有什么问题?身份验证有问题吗?如果我尝试在命令行中访问它,该 url 也不起作用,但该代码在我的 XAMPP 中有效,并且该 url 在浏览器中有效。
我真的很感激任何帮助!!! 提前谢谢!
【问题讨论】:
-
[rest url]是否真的设置为 URL? -
是的,这是我的网址 (http://.....)
标签: php apache authentication curl server