当前位置: 动力学知识库 > 问答 > 编程问答 >

apache - PHP curl working on XAMPP, but not on Server

问题描述:

I transferred my application from my development environment XAMPP to our old Ubuntu Web Server (Ubuntu 12.04.5 LTS). On the server, PHP 5.3 is installed.

I enabled curl, which is working.

When I try to run my code, an error occurs "Host not found". I already tried to debug the curl with the help of this article but I'm not able to figure out what the problem is.

I tried for example the following code:

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);

The code above contains the debugging code from the article. The following is the output (additionally to the $response output which contains the error "host not found"):

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

Does anybody has an idea, what might be the problem in there? Is there a problem with the authentication? The url does not work if I try to access it in the command line either, but the code works in my XAMPP and the url works in the browser.

I really appreciate any help!!!

Thank you in advance!

网友答案:

Finally I found out, that it works if I use the pure IP instead of the domain name. I'm not familar with network stuff, but it seems that there was a problem with DNS.

But by using the IP in the URL, everything works fine.

分享给朋友:
您可能感兴趣的文章:
随机阅读: