I have a script with line:
$id = strtolower(implode('',file($ip_service . $ip)));
When executed the file function will be like file(http://www.url.com/filename.php?119.160.120.38)
On server A it works fine but on Server B it gives following error:
file(http://www.url.com/filename.php?xxx.xxx.xxx.xxx) [function.file]: failed to open stream: Connection timed out in /home/path/filename.php on line 22
Line 22 is the above line of code.
Server A has PHP 4.4.6, Server B has 4.4.8
Any help will be highly appreciated.
Using file() and/or file_get_contents() for accessing files across the internet is fairly error prone. For example, I don't think they follow redirects. The timeout period is also very short, not the typical network timeout. It's also difficult to do error capture to see why it failed.
I always use CURL for accessing files over the network. It takes a few more lines of code, but is much more reliable. Note, PHP CURL support may not be installed in your setup.
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, 'http://www.url.com/filename.php?119.160.120.38');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
if ( $result==false ) {
echo curl_errno($ch).' '.curl_error($ch);
}
curl_close($ch);
Obviously you would want to do something beside echo out the error message if there was an error. The $result variable will have the contents of the file if it succeeds.
Your server couldn't establish a tcp/ip connection to the server hosting www.url.com in a given time period (20 seconds? 30 seconds? What ever the default is or what you have specified as timeout). The other server didn't even reject the connection actively, there just wasn't any response at all. Could be e.g. a firewall issue where some or all of your or the other server's packets where silently dropped.
Related
I have a server connected to my company via OpenVPN and I am trying to retrieve JSON from another server using PHP's curl command.
Running the most stripped back version of the code in PHP's interactive mode from the command line (php -a), works fine:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'status.int.companyname.com');
curl_exec($ch);
{...expected json response after 2 seconds}
Howver when I run this from within Apache, the script will just hang.
Setting some timeouts and outputting the error
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
echo 'Error: '.curl_error($ch);
Gives the error as
Error: Resolving timed out after 10517 milliseconds
Initially I thought this was a DNS-related issue, but changing the URL to use the IP address or specifying the address to resolve to
curl_setopt($ch, CURLOPT_RESOLVE, [ 'status.int.companyname.com:80:10.10.0.5']);
yeilded a connection error:
Error: Connection timed out after 10001 milliseconds
My next thought was that Apache was using the wrong interface. The VPN is the default gateway, so works fine with everything else but I tried forcing it with
curl_setopt($ch, CURLOPT_INTERFACE, '10.33.0.105');
This made no difference. After reading around a few other posts, I tried adding the following in various combinations but it made no difference.
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
file_get_contents has the same problem
I don't inderstand it. Curl on the command line works and PHP in interactive mode work fine. It just doesn't work when the PHP script is executed via Apache.
Other things I've tried:
starting Apache after the VPN is connected
setting Apache to only bind to IPv4
Resolv.conf contains one nameserver, which is the correct one for the company VPN
The differences between the CLI and Apache versions of php.ini are as expected:
-disable_functions =
+disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
-expose_php = On
+expose_php = Off
-memory_limit = -1
+memory_limit = 128M
Has anyone seen this before? I've lost a few hours of my day on this now and it's driving me mad!
Thanks
My curl request is..
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
//curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_INTERFACE,$proxy);
curl_setopt($ch,CURLOPT_PORT, '8888');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
pr($ch);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
Error Resource id #12
i am trying to fetch data from amazon and have a list of ip address, and port number.
sending request from diff port number preventing blocking from amazon...
It seems that your curl client is getting some response from server that it can not understand, Error#12 means: CURLE_FTP_WEIRD_USER_REPLY
here is the PHP Manual link: http://php.net/manual/en/function.curl-errno.php
and description of error: http://web.mit.edu/jhawk/mnt/ss.b/curl-7.10.5/docs/libcurl/libcurl-errors.html
You can use curl_error to capture the error How to catch curl errors in PHP
Other thing to consider here is your proxy, is it allowed to access the host and port you are trying to access and you access the host without proxy?
CURLE_FTP_WEIRD_SERVER_REPLY (8)
The server sent data libcurl couldn't parse. This error code is used for more than just FTP and is aliased as CURLE_WEIRD_SERVER_REPLY since 7.51.0.
CURLE_REMOTE_ACCESS_DENIED (9)
We were denied access to the resource given in the URL. For FTP, this occurs while trying to change to the remote directory.
CURLE_FTP_ACCEPT_FAILED (10)
While waiting for the server to connect back when an active FTP session is used, an error code was sent over the control connection or similar.
CURLE_FTP_WEIRD_PASS_REPLY (11)
After having sent the FTP password to the server, libcurl expects a proper reply. This error code indicates that an unexpected code was returned.
CURLE_FTP_ACCEPT_TIMEOUT (12)
During an active FTP session while waiting for the server to connect, the CURLOPT_ACCEPTTIMEOUT_MS (or the internal default) timeout expired.
for more details
enter link description here
CURLE_FTP_ACCEPT_TIMEOUT (12)
During an active FTP session while waiting for the server to connect, the CURLOPT_ACCEPTTIMEOUT_MS (or the internal default) timeout expired.
Does anyone know what happen on the error as below when calling curl even call yahoo page:
Error code 6 :Couldn't resolve host 'http://www.yahoo.com'; No data of requested type
PHP
$sendurl = "http://www.yahoo.com";
$ch = curl_init($sendurl);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
$result = curl_exec($ch);
if(!$result){
echo $curl_errno($ch).":".curl_error($ch);
}
This error mean that curl can't resolve host name.
There is problem with DNS server on computer where you run this script.
If you run script on your local computer then check it just open www.yahoo.com in your browser.
If you run script on remote server, then you should login to server by SHH and check ping www.yahoo.com. Or ask your hosting support team about this issue.
I think you're missing options for allowing redirects and https. Check it in your browser -> you will get redirected to https URL.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
I send an item code to a web service in xml format using cUrl(php). I get the correct response in localhost, but when do it server it shows
cURL Error (7): couldn't connect to host
And here's my code:
function xml_post($post_xml, $url)
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
// curl_setopt($ch, CURLOPT_PORT, $port);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
echo "Data received\n";
}
curl_close($ch);
echo $data;
}
I send the item code to the tally and fetch the details from it. I tried using both the versions php 4+ and php5+, nothing works out Any solution.
CURL error code 7 (CURLE_COULDNT_CONNECT)
is very explicit ... it means Failed to connect() to host or proxy.
The following code would work on any system:
$ch = curl_init("http://google.com"); // initialize curl handle
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
print($data);
If you can not see google page then .. your URL is wrong or you have some firewall or restriction issue.
“CURL ERROR 7 Failed to connect to Permission denied” error is caused, when for any reason curl request is blocked by some firewall or similar thing.
you will face this issue when ever the curl request is not with standard ports.
for example if you do curl to some URL which is on port 1234, you will face this issue where as URL with port 80 will give you results easily.
Most commonly this error has been seen on CentOS and any other OS with ‘SElinux’.
you need to either disable or change ’SElinux’ to permissive
have a look on this one
http://www.akashif.co.uk/php/curl-error-7-failed-to-connect-to-permission-denied
Hope this helps
If you have tried all the ways and failed, try this one command:
setsebool -P httpd_can_network_connect on
In PHP, If your network under proxy. You should set the proxy URL and port
curl_setopt($ch, CURLOPT_PROXY, "http://url.com"); //your proxy url
curl_setopt($ch, CURLOPT_PROXYPORT, "80"); // your proxy port number
This is solves my problem
In my case I had something like cURL Error (7): ... Operation Timed Out. I'm using the network connection of the company I'm working for. I needed to create some environment variables. The next worked for me:
In Linux terminal:
$ export https_proxy=yourProxy:80
$ export http_proxy=yourProxy:80
In windows I created (the same) environment variables in the windows way.
I hope it helps!
Regards!
Are you able to hit that URL by browser or by PHP script? The error shown is that you could not connect. So first confirm that the URL is accessible.
Check if port 80 and 443 are blocked. or enter - IP graph.facebook.com and enter it in etc/hosts file
you can also get this if you are trying to hit the same URL with multiple HTTP request at the same time.Many curl requests wont be able to connect and so return with error
This issue can also be caused by making curl calls to https when it is not configured on the remote device. Calling over http can resolve this problem in these situations, at least until you configure ssl on the remote.
In my case, the problem was caused by the hosting provider I was using blocking http packets addressed to their IP block that originated from within their IP block. Un-frickin-believable!!!
For a couple of days I was totally blocked on this. I'm very very new to networking/vms but was keen to try set it up myself instead of paying a hosting company to do it for me.
Context
I'm rebuilding the server side for an app that uses php routines to return various bits of data from internal sources as well as external APIs for a map based app. I have started an Oracle VM instance and have installed/set up Apache and php. All running totally fine, until one of my php routines tries to execute a cURL. I start implementing error logging to find that I don't even get a message - just '7', despite implementation being very similar to the above. My php routine accessing an internal file for data was running successfully so I was fairly sure it wasn't an Apache or php issue. I also checked my Apache error logs, nothing telling.
Solution
I nearly gave up - there's talk on disabling SELinux above and in other articles, I tried that and it did work for my purposes, but here's a really good article on why you shouldn't disable SELinux https://www.electronicdesign.com/technologies/embedded-revolution/article/21807408/dont-do-it-disabling-selinux
If temporarily disabling it works and like me you don't want to do this (but it confirms that SELinux is blocking you!), I found a neat little command that actually prints out any SELinux issues in a more readable fashion:
sealert -a /var/log/audit/audit.log
This returned the following:
found 1 alerts in /var/log/audit/audit.log
--------------------------------------------------------------------------------
SELinux is preventing php-fpm from name_connect access on the tcp_socket port 443.
Great, I now get a bit more information than just '7'. Reading further down, I can see it actually makes suggestions:
***** Plugin catchall_boolean (24.7 confidence) suggests ******************
If you want to allow httpd to can network connect
Then you must tell SELinux about this by enabling the 'httpd_can_network_connect' boolean.
Do
setsebool -P httpd_can_network_connect 1
This has been mentioned further above but now I have a bit more context and an explanation as to what it does. I run the command, and I'm in business. Furthermore, my SELinux is still set to enforcing, meaning my machine is more secure.
There are many other suggestions logged out, if you're blocked it might be worth logging out/checking out /var/log/audit/audit.log.
I'm having some problems with my PHP server. Most of the functions when run gives the same error.
Warning: fopen(http://www.ietf.org/rfc/rfc2475.txt) [function.fopen]: failed to open stream:
A connection attempt failed because the connected party did not properly respond after a
period of time, or established connection failed because connected host has failed to respond. D:\inetpub\vhosts\coolfbapps.in\httpdocs\test\merger2.php on line 3
Fatal error: Maximum execution time of 30 seconds exceeded in D:\inetpub\vhosts\coolfbapps.in\httpdocs\test\merger2.php on line 3
The same error comes when i use imagecreate functions, get_image functions.
I talked to service providers but they said I should tell them the cause of this error so that they can rectify it. Please se if any one can make out what changes should be done to server to remove those errors.
CODE USED
$ch = curl_init("http://www.gravatar.com/avatar/95111e2f99bb4b277764c76ad9ad3569?s=32&d=identicon&r=PG");
$fp = fopen("http://www.ietf.org/rfc/rfc2475.txt", "r");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Sorry for this, but I'm unable to comment yet. You need to post the code that is causing these errors. Are you explicitly requesting that URL?
To debug this, you should logon to the server and attempt to request the file to see if you're actually able to have outbound connections on port 80. To do this on a linux server just run
wget http://www.ietf.org/rfc/rfc2475.txt
.. and see if it fails or not. If it does, you need to talk to your hosting provider / ISP.
If you don't have access to the server you could simply try (in PHP):
<?php
file_get_contents('http://www.google.com/'); // Google so that it's not the same URL
?>
If there's an error, same as above.
The first error (the fopen() one) is caused because of a timeout in the response of the server you're trying to load the data from. i.e. the server is taking too long to respond, so the connection times out.
The second error is that the script is running for too long. PHP has a setting called max_execution_time so that a script can't eat up all the resources on a server. Your server has a setting to allow 30 seconds to execute, or die() with a fatal error.
Seems your server can't connect to these sites. Perhaps your server is not allowed to start outgoing connections or is blocking that outgoing HTTP traffic somehow?
I bet this isn't a PHP problem but instead has to do with your server connection. If you have SSH access, try to open these URLs from the command line without PHP.