简体   繁体   中英

php/curl: Timeout problem when remote server curling itself

I have a simple "server" script that just echos a line:

<?php 
echo "Server script on http://".$_SERVER['SERVER_NAME']." saying hello!";

And a "client" script curling the server script:

<?php 
echo "Client script";

$url = "http://localhost/server.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$data = curl_exec($ch);
curl_close($ch);        

echo "<br/>Data returned from server script:";
echo "<hr/>";
echo $data;

The last alternative causes no response from the remote server, just the browser chewing until timeout. Any ideas?

The problem is apparently on the remote server.

If the remote server is a shared server and you are using localhost there instead of a full url, then the local machine will not know which local website to call.

Another possibility is that your remote server does not allow the server processes to request data from elsewhere. To test this you should try to call your local machine (if reachable from the internet) or a second machine and call that from the server.

You have an error

$ch = curl_init();

should be

$ch = curl_init($url);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM