I have a URL - http://www.xxx.xxx.xxx/
This URL, I'm able to open through the browser. I'm even able to call this URL via cURL from my machine, but when I'm calling it from the code on the same server and domain, it gives me an error - error: couldn't connect to host
Following is my code -
function get_xml_via_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
echo $url;
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
/*curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);*/
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$returned = curl_exec($ch);
if(curl_errno($ch)) {
echo 'error: ' . curl_error($ch);
}
curl_close($ch);
// $xml === False on failure
$xml = simplexml_load_string($returned);
return $xml;
}
Like I said, on localhost this works. I can directly open the URL via the browser. But when I deploy the code on the same server and try calling via cURL, it gives me an error - error: couldn't connect to host
Updated Code -
I modified the code on my server to -
function get_xml_via_curl($url) {
$url = " http://www.xxx.xxx.xxx.xxx/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$returned = curl_exec($ch);
if(curl_errno($ch)) {
echo 'error: ' . curl_error($ch);
}
curl_close($ch);
$xml = simplexml_load_string($returned);
return $xml;
}
And the error from curl changes to following -
error: Protocol http not supported or disabled in libcurl
Your updated code is not working because of the white space before http in $url = " http...
The original code is probably not working because of some firewall or nameserver issue. Try to visit the URL you want to load directly in a browser ON THE SAME SERVER. That will probably fail too.
You need to fix the server to accept requests from the localhost.
What can also help you on your way is to check the web server and firewall log files. See why the requests are being declined. And even temporarily just disable the firewall to rule it out.
I have faced the same, PHP curl throwing 'Connection timeout error' because of same domain and tried all other options, but nothing worked out.
Finally I got the answer and fixed it. Just run below command (ubuntu)
# sudo ifconfig lo up
For Cent OS:
# sudo ip link set lo up
# sudo ip addr show
"lo" is the loopback interface. This is a special network interface that the system uses to communicate with itself.
try:
$url="//your url";
echo get_xml_via_curl($url);
Related
Explanation
I made a simple REST api in Java (GET).
Postman works (both localhost and IPv4)
curl from command line works (both localhost and IPv4)
External request from a different city works (IPv4)
Expected
To have PHP curl work on the localhost
Actual
For some reason PHP curl on IPv4 works, but localhost does not work
PHP curl output error
Failed to connect to localhost port 8080: Connection refused
curl error: 7
Code
$url = 'http://localhost:8080/api/user';
$curl = curl_init($url);
echo json_decode(curl_exec($curl));
I have tried (from the top of my head, no specific order)
curl_setopt ($curl, CURLOPT_PORT , 8080);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
For those ones who are using Docker, Vargant and so on.
You got Failed to connect to localhost port 8080: Connection refused error, because you are trying to connect to localhost:8080 from inside virtual machine (eq. Docker), this host isn't available inside the Docker container, so you should add a proxy that can reach a port from the out.
To fix this issue add the next line of code after your curl_init():
curl_setopt($ch, CURLOPT_PROXY, $_SERVER['SERVER_ADDR'] . ':' . $_SERVER['SERVER_PORT']);
Here is a full example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $APIUrl);
if ($_SERVER['HTTP_HOST'] == 'localhost:8080') {
// Proxy for Docker
curl_setopt($ch, CURLOPT_PROXY, $_SERVER['SERVER_ADDR'] . ':' . $_SERVER['SERVER_PORT']);
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error = curl_error($ch);
}
curl_close($ch);
I'm not sure if this counts as an answer but I just restarted the linux VM and now its working...
Not sure if it's still usefull for you, but I've faced with almost same problem. I've started php server using php -S localhost:5500 or with VS-code extention, and try to connect via curl to same host, and it taken almost infinite time with no response. Solution was simple (if you understand russian, or can use translator you can find full article here).
So, from this article:
This script will not work with the default WPN-XM v0.8.6 setting because there is only one php -C gi process listening in the background, but your example requires (at least) two of them. php -C gi is already in use by the script making the curl request and therefore Nginx cannot redirect it to php -C gi. This means that you will first come across a blank page with a loading indicator and then remove the connection timeout.
The main problem is that php -C gi doesn't automatically spawn new processes (if needed). The issue is discussed here: https://github.com/WPN-XM/WPN-XM/issues/323
So the solution for me was to create another php server with a different port (for example 5500 and 5550) and everything started working.
I having extreme difficulty with PHP curl. I am attempting to open a site: https://www.novaprostaffing.com/np/index.jsp through PHP curl, but it keeps yielding the following error: "Unknown SSL protocol error in connection to www.novaprostaffing.com"
My function is as follows:
function getUrl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$buffer = curl_exec($ch);
if (!$buffer)
{
echo "cURL error number:" .curl_errno($ch);
echo " and url is $url and cURL error:" . curl_error($ch);
}
curl_close($ch);
return $buffer;
}
I have attempted multiple fixes including:
Forcing curl to version 3
Setting CURLOPT_SSL_VERIFYPEER & CURLOPT_SSL_VERIFYHOST to 0
Checking to see if was on curl 7.34. I was told there was a bug on this version, but I am on curl 7.19.1
None of the above worked. If you have any idea how to fix this, it would be much appreciated!
Try setting the cURL option CURLOPT_SSLVERSION. I had the same problem a while ago, this did the trick for me :)
curl_setopt($ch, CURLOPT_SSLVERSION, 3); // 1, 2 or 3
The server speaks only TLS 1.0 and trying to connect with SSL 2.0 or SSL 3.0 will cause the error you see. This means setting the version to 3 is exactly the wrong thing with this server. Apart from that the certificate chain is incomplete. The server only provides the leaf certificate, not the intermediate certificates until the trusted root. This will cause verification to fail.
Did you try some other https url and see if that worked ?
Here are 3 common causes
Destination Site Does Not Like the Protocol
The Destination Site Does Not Like the Cipher
The SSL Private Key Has Expired
my curl code show me a blank page
<?
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,"http://mysite/scripts/showsomething.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
$core = explode('<!--- BEGIN -->', $result);
$main = explode('<!--- END -->', $core[1]);
echo $main[0];
?>
this code works fine on localhost, but not on server...
There can be several reasons behind your problem.
1) Change <? into <?php and see whether it works or not.
2) For a short test, run this code from your server and check whether it shows you the output or not.
<?php
echo "sabuj";
?>
3) Some site seek for useragent string on their website request. When they found no useragent, they use to return blank page. You can overcome this with setting an useragent like below.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
4) Another thing you can do is, access the server with ssh client(if you have any) and run the url with wget tool and see whether you can download your page or not.
wget "http://yoursite/page/blabla/...php"
5) Finally, run your curl code with verbose mode enabled. It will help you to debug your curl requests.
curl_setopt($ch, CURLOPT_VERBOSE,true);
Working Code...
$ch = curl_init();
$timeout = 10;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
Hope it helps...
Turn your error reporting all the way to 11.
error_reporting(E_ALL);
This will tell you a little more about the issue you are facing. The most common culprit when working with curl on a dev environment and moving to a server is a missing curl package.
You can check to see if you have curl installed by doing the following:
if(!function_exists('curl_version')) {
throw new Exception('Curl package missing');
}
Thus "PHP Fatal error: Call to undefined function curl_init() in..." is a very common error that is thrown.
Some additional debugging tips are to..
print_r the response of curl_exec($handle);
print_r curl_error($handle) this will give you a curl error code.
setup a curl proxy using set_opt and CURLOPT_PROXY, set the value to your ip:port, open the port 8888 on your router and install charles proxy. This will show you an exact printout of the request and response.
I've read the previous answers and it seems that your code is fine but problem is connectivity to remote server, please try this:
<?
//debug
error_reporting(E_ALL);
ini_set('display_errors', 1);
//debug end
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_URL,"http://mysite/scripts/showsomething.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
echo result;
?>
what does it output ?
Notes:
Do you administer the other server?
if not, is there a possibility that your ip was blocked by the remote server?
Does script http://mysite/scripts/showsomething.php contains any errors ?
If you're able to edit it, please enable php errors on showsomething.php by adding the following code at the top of it :
error_reporting(E_ALL);
ini_set('display_errors', 1);
I've solved the problem by adding the following code.
The problem is SSL.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
I hope it helps.
I have tested my local server with a proxy and my friends can all load the url in their browser, but when I try to use cURL on a separate server, it fails connecting.
Here is the URL, it loads on both my browser and using my friend's internet:
http://pc.stephen3.com:8080/gameq.php
Here is the code I am using on a separate server, it successfully retrieves all other URLs (like google and yahoo)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://pc.stephen3.com:8080/gameq.php");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$err = curl_error($ch);
$resp = curl_exec($ch);
echo $resp;
echo $err;
But it returns nothing on my home server's url, and
curl_error($ch);
(did say, now says nothing) "couldn't connect to host"
Any ideas what it could be? The url works everywhere, but in cURL. The code works everywhere but with the URL.
EDIT: It occurred to me that it may be because of the outbound traffic on my external server being on port 8080 instead of 80 for regular servers, will have to test if my hoster allows curl on other ports.
Maybe you have to remove port number from URL and configure CURLOPT_PORT option.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://pc.stephen3.com/gameq.php"); // port removed
curl_setopt($ch, CURLOPT_PORT, 8080); // that line added
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$err = curl_error($ch);
$resp = curl_exec($ch);
echo $resp;
echo $err;
You may also use curl_getinfo($ch) function for more debug information.
$report=curl_getinfo($ch);
echo "curl_getinfo returned: <br>".$report;
It turns out my host (iPower) restricts cURL to port 80 :(
EDIT : I found out that support lied, saying only port 80 is allowed. In fact, they allow port 80 AND 990 (SSL) but you can use port 990 without actually having SSL on. Works perfectly.
cURL + proxy noob here, having a hard time. I'm having trouble trying to retrieve a web page from a remote secure server via a proxy. Everything has apparently been set up correctly by a remote dev, such that the following command line instruction works and returns what we're looking for:
curl -k --socks5-hostname localhost:xxxx https://hostname/
However, the following PHP does not echo the requested webpage. Instead it echoes the error 'Couldn't resolve host name':
$proxy = 'localhost:xxxx';
$url = 'https://hostname/';
//$proxyauth = 'user:password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
$curl_scraped_page = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error)
echo $error;
elseif ($curl_scraped_page)
echo $curl_scraped_page;
If the $url is changed to a public page, such as Google, the request is successful and everyone is happy.
The connection requires an SSH tunnel if that changes anything at all. The tunnel is open and functioning, as proven by the command line request succeeding.
Is there something obvious that is being missed here?
You need to set option CURLOPT_PROXYTYPE to CURLPROXY_SOCKS5_HOSTNAME, which sadly wasn't defined in old PHP versions, circa pre-5.6; if you have earlier in but you can explicitly use its value, which is equal to 7:
curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
In the option CURLOPT_PROXYTYPE you need to set CURLPROXY_SOCKS5_HOSTNAME option instead of CURLPROXY_SOCKS5.
In this case, the DNS query (for hostname resolving) will be sent to SOCKS proxy and not resolved in the local network.
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
This constant available since PHP 5.5.23 and PHP 5.6.7 and cURL 7.18.0, so you can simply use it.