How to Resolve Curl Error (7): Couldn't Connect to Host

curl: (7) couldn't connect to host What is wrong in my settings?

Apparently, your server is not listening on port 9201.

You need to first verify that if your server has successfully started listening on your designated port. As yudong shen suggested, you may use netstat command to verify this.

For example:

To list all the <ip:port> pairs along with their state and their respective process:

$ netstat -anp

Observe the Local address, State and PID/Program name columns to identify your process and its designated <ip:port> with state.

To check if the designated port is used by a process irrespective of its state:

$ netstat -anp | grep :9201

To make sure that the port is open for listening:

$ netstat -anp | grep LISTEN | grep :9201

You may also use a REST Client such as Google Chrome's Postman to interact with your web server from a GUI.

curl give error curl: (7) couldn't connect to host

This may be your issue. Because curl is set to port 6821, it may be blocked. Does the same request to port 80 work? Check out this other thread.

How to resolve cURL Error (7): couldn't connect to host?

How to Resolved : cURL error (7): Couldn't connect to serverConnection Failure

This is an A2 hosting server problem (port 8080 problem)
Generated ticket and Resolved now.

PHP/cURL Error 7; Couldn't connect to host

So in order to get this to work, I had to put a bottleneck on the function to slow it down a bit. As Andrewsi suggested, the remote site was cutting me off for downloading images too quickly. In order to bottleneck the function, I FTP'd each image to a remote server (since this was required anyway).

Final function looks like this:

function downloadImage($location, $imagesPath, $imageName, $ch3, $feedFTPinfo) {

//Location fix
//$location = str_replace(" ", "%20", $location);

$url = $location;
$path = $imagesPath . $imageName;

$fp = fopen($path, 'w');
$ch2 = curl_init(); // Initiate cURL for downloading images

//Setup the cURL options for the second handle ($ch2)
curl_setopt($ch2, CURLOPT_URL, $url);
curl_setopt($ch2, CURLOPT_FILE, $fp);
curl_setopt($ch2, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);

//Execute the cURL session
$data2 = curl_exec($ch2);

//Resize Images
$image = new SimpleImage();
$image->load($url);
$imageNameSm = str_replace(".jpg", "", $imageName);
$imageNameSm = $imageNameSm."_sm2.jpg";
$image->resizeToWidth(120);
$image->save($imagesPath . $imageNameSm);
$smPath = $imagesPath . $imageNameSm;

//Find out if there were any issues
if ($data2 === false) {
echo "DownloadImage cURL failed 1: (" . curl_errno($ch2) . ") " . curl_error($ch2) . "<br/>";
//exit;
} else {
//There weren't any issues downloading the file, move it to the speficifed ftp server

if (!empty($feedFTPinfo)) {

$localfile = $path;
$fp = fopen($localfile, 'r');

//Setup the options for the 3rd handle
curl_setopt($ch3, CURLOPT_URL, $feedFTPinfo.$imageName);
curl_setopt($ch3, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately
curl_setopt($ch3, CURLOPT_UPLOAD, 1);
curl_setopt($ch3, CURLOPT_INFILE, $fp);
curl_setopt($ch3, CURLOPT_INFILESIZE, filesize($localfile));

//Execute the 3rd cURL handle
$data3 = curl_exec($ch3);

//Find out if there were any issues with the execution
if ($data3 === false) {
echo "Uploading the image via FTP failed: (" . curl_errno($ch3) . ") " . curl_error($ch3) . "<br/>";
//exit;
}

$localfile = $smPath;
$fp = fopen($localfile, 'r');

//Setup the options for the 3rd handle
curl_setopt($ch3, CURLOPT_URL, $feedFTPinfo.$imageNameSm);
curl_setopt($ch3, CURLOPT_CONNECTTIMEOUT, 0); //Wait indefinitately
curl_setopt($ch3, CURLOPT_UPLOAD, 1);
curl_setopt($ch3, CURLOPT_INFILE, $fp);
curl_setopt($ch3, CURLOPT_INFILESIZE, filesize($localfile));

//Execute the 3rd cURL handle
$data3 = curl_exec($ch3);

//Find out if there were any issues with the execution
if ($data3 === false) {
echo "Uploading the small image via FTP failed: (" . curl_errno($ch3) . ") " . curl_error($ch3) . "<br/>";
//exit;
}




}


}

curl_close($ch2); //Close the cURL handle that downloads images
fclose($fp);
}

If you don't need to ftp somewhere to create the bottleneck, you could use php's sleep (seconds) or usleep (microseconds) functions within your downloadImage function to create a similar bottleneck.

  • sleep documentation:
    http://php.net/manual/en/function.sleep.php
  • usleep documentation:
    http://php.net/manual/en/function.usleep.php

Hope this theory helps someone else out.

CURL error couldn't connect to host on http server only

i just changed the IP of the url to "localhost" and it worked :)
$requestedUrl = http://localhost/myproject/index.php?entryPoint=viewReport&record=.....etc..



Related Topics



Leave a reply



Submit