Copy php failed to open stream connection timed out

file_get_contents giving failed to open stream: Connection timed out error for file on same server

I am trying to have a file on my server send post data as if coming from a form («calling_file.php») to another file on the same server («processing_file.php»). Here is the code:

$url = 'https://www.example.com/path/to/processing_file.php'; $method = 'POST'; $eol = "\r\n"; // Create a stream $opts = array( 'http' => array( 'timeout' => 30, 'method' => $method, 'header' => 'Content-Type: application/x-www-form-urlencoded' . $eol, 'content' => http_build_query($xmit) ) ); $context = stream_context_create($opts); // Open the file using the HTTP headers set above $result = file_get_contents($url, false, $context); //filename, use_include_path, context //echo($result); $result_decode = json_decode($result,true); 
array( 'dx' => json_encode('[,,,,]'), 'period' => 5, 'phase_code' => 3, 'site_id' => 155 ) 

When I echo out $opts, everything looks right. http_build_query($xmit) is creating what looks to be valid post data. I have this set in processing_file.php and calling_file.php:

ini_set('allow_url_include', '1'); ini_set('allow_url_fopen', '1'); 

When I paste: http://www.example.com/path/to/processing_file.php directly in my browser, the file opens with 200 status, all good. I tried setting the owner of processing_file to be the apache user, and set chmod to 775 on it. However, when calling_file.php tries to call processing_file.php with the above code, I get this:

Warning: file_get_contents(https://www.example.com/path/to/processing_file.php): failed to open stream: Connection timed out in /var/www/example.com/functions/path/to/other/file/calling_file.php on line 219 
$result = file_get_contents($url, false, $context); //filename, use_include_path, context 

Источник

Читайте также:  Python passing file objects

failed to open stream: Connection timed out when getting content from another website

For more than 2 years I had paid agreement with another website to be able to get their content via my script using Simple_html_DOM. Now suddenly without any warning and still under contract with them Im getting failed to open stream: Connection timed out no matter what Im using- simple_html_DOM, cURL, file_get_content. I even tried snoopy library to simulate web browser, still getting Connection timed out. They somehow blocking connections. Its not IP blocking as well as I tried from several different servers with same results. Their website is loading fine in my web browser so no problems there. Is there any other way I could get content from that website? As I paid money for it and they blatantly ignoring me after taking my money.

So you are receiving a «Connection timed out» in all of the tried cases and no response at all? In most cases web hosts perform header checks to see if the accessing client is a web browser i.e by checking the User-Agent. Best approach would be to mimic all request headers your browser sends in a cURL request and see if that still results in a timeout. Also make sure that it’s not something else on your side e.g. that you use a proxy in your web browser whereas you are using none in your programmatic tests.

2 Answers 2

The server is probably blocking requests based on (absence of valid) user agent header ( User-Agent: ). Basically this header self-identifies the to server what it is: a browser, a bot, spider or app etc.

Читайте также:  Print with php code

You can try using cURL to send the same kinds of header the server would expect from a typical browser, using curl_setopt and the CURLOPT_USERAGENT option (docs here).

$url = "https://example.com"; // we're going to impersonate Chrome 74 on MacOS in this example. $user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"; $ch = curl_init(); // this is where we set the option to send the user agent header curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL,$url); $result=curl_exec($ch); 

If that still doesn’t work, make sure you don’t need cookies or login credentials.

Источник

file_get_contents — Connection timed out

Mostly probably your server cannot connect to an external resource, for example, because of firewall restrictions.

I had the same problem, couldn’t download using file_get_contents(), but using curl on the command line for the same URL worked fine. Turned out it tried to connect over IPv6 which failed.

I solved it by disabling IPv6 in my kernel parameters.

Rather than turning off IPv6, you should try to fix it. You can also make file_get_contents() try connection over IPv6 and then also over IPv4 if that fails.

file_get_contents does not work well at all with getting remote files and should not be used. It does not deal with slow network connections or redirects, and does not return error codes. You should use curl instead to fetch remote files.

I assure you that file_get_contents() works just fine with getting remote files, and can be used in most situations. Also, it does follow proper HTTP redirects, but there have been bugs reported recently with this feature, which I am assuming is what you are referring to. Finally, yes, if you need the HTTP status codes then you should use something more robust. If you simply need to catch an error then you can use file_get_contents() just fine. Again, this is fine for plenty of situations, but certainly not all, as you have pointed out.

Down voting a correct answer? I didn’t say it didn’t work, I said it doesn’t work well. curl is a much more reliable and robust tool to use, as you stated. The timeout error experienced most likely would not happen with curl since curl defaults to the network timeout (i.e. 2 minutes).

It might be server side issue, might be your server cannot communicate with other server remotely. You have to communicate with server administrator.

For some reason file_get_contents() fails intermittently on some websites that support IPv6 (maybe 20% of the time, the command times-out). Although disabling IPv6 resolves the issue, that is not the best solution as more sites move to IPv6.

I wrote a simple PHP gethtml() function that works around the issue using wget. This will automatically use IPv6 when IPv4 is not available. A minor drawback is that it uses an external command, but I think it is preferable to disabling IPv6.

If wget is not already installed on your distribution, you can install it as follows:

$myhtml = gethtml("http://example.com"); //use instead of file_get_contents() due to inconsistent IPv6 performance function gethtml($url)

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP copy composer: Failed to open stream #49

PHP copy composer: Failed to open stream #49

Comments

When I try to install PHP 7.4.4 on Mint 19.3, I get the following error:

(. ) Warning: copy(https://getcomposer.org/installer): failed to open stream: Connection timed out in Command line code on line 1 Warning: hash_file(composer-setup.php): failed to open stream: No such file or directory in Command line code on line 1 Installer corrupt Warning: unlink(composer-setup.php): No such file or directory in Command line code on line 1 Could not open input file: composer-setup.php 

Is there another way to install the composer or some requirement that is not defined in the README ?

The text was updated successfully, but these errors were encountered:

If you still have problems, please open this again.

I’m having the same issue on MacOS 11.6

Build complete. Don't forget to run 'make test'. Running make install. Installing PHP CLI binary: /Users/Emptyfruit/.asdf/installs/php/7.4.24/bin/ Installing PHP FPM binary: /Users/Emptyfruit/.asdf/installs/php/7.4.24/sbin/ Installing PHP CGI binary: /Users/Emptyfruit/.asdf/installs/php/7.4.24/bin/ Installing phpdbg binary: /Users/Emptyfruit/.asdf/installs/php/7.4.24/bin/ Installing build environment: /Users/Emptyfruit/.asdf/installs/php/7.4.24/lib/php/build/ Installing helper programs: /Users/Emptyfruit/.asdf/installs/php/7.4.24/bin/ mkdir: /Users/Emptyfruit/.asdf/installs/php/7.4.24/bin: File exists mkdir: /Users/Emptyfruit/.asdf/installs/php/7.4.24/bin: File exists program: phpize Installing shared extensions: /Users/Emptyfruit/.asdf/installs/php/7.4.24/lib/php/extensions/no-debug-non-zts-20190902/ mkdir: /Users/Emptyfruit/.asdf/installs/php/7.4.24/var/log: File exists program: php-config Installing PHP CLI man page: /Users/Emptyfruit/.asdf/installs/php/7.4.24/php/man/man1/ Installing PHP CGI man page: /Users/Emptyfruit/.asdf/installs/php/7.4.24/php/man/man1/ mkdir: /Users/Emptyfruit/.asdf/installs/php/7.4.24/var/run: File exists Installing PEAR environment: /Users/Emptyfruit/.asdf/installs/php/7.4.24/lib/php/ Installing man pages: /Users/Emptyfruit/.asdf/installs/php/7.4.24/php/man/man1/ mkdir: /Users/Emptyfruit/.asdf/installs/php/7.4.24/php/man/man1: File exists page: phpize.1 Installing PHP FPM defconfig: /Users/Emptyfruit/.asdf/installs/php/7.4.24/ Installing phpdbg man page: /Users/Emptyfruit/.asdf/installs/php/7.4.24/php/man/man1/ Installing PDO headers: /Users/Emptyfruit/.asdf/installs/php/7.4.24/include/php/ext/pdo/ page: php-config.1 /var/folders/6j/dvct08wd1vbcw5xyr4hrmnd40000gn/T/php-src-php-7.4.24/build/shtool install -c ext/phar/phar.phar /Users/Emptyfruit/.asdf/installs/php/7.4.24/bin/phar.phar ln -s -f phar.phar /Users/Emptyfruit/.asdf/installs/php/7.4.24/bin/phar [PEAR] Archive_Tar - installed: 1.4.14 [PEAR] Console_Getopt - installed: 1.4.3 Installing PHP FPM man page: /Users/Emptyfruit/.asdf/installs/php/7.4.24/php/man/man8/ [PEAR] Structures_Graph- installed: 1.1.1 [PEAR] XML_Util - installed: 1.4.5 warning: pear/PEAR dependency package "pear/Archive_Tar" installed version 1.4.14 is not the recommended version 1.4.4 Installing PHP FPM status page: /Users/Emptyfruit/.asdf/installs/php/7.4.24/php/php/fpm/ [PEAR] PEAR - installed: 1.10.13 Warning! a PEAR user config file already exists from a previous PEAR installation at '/Users/Emptyfruit/.pearrc'. You may probably want to remove it. Wrote PEAR system config file at: /Users/Emptyfruit/.asdf/installs/php/7.4.24/pear.conf You may want to add: /Users/Emptyfruit/.asdf/installs/php/7.4.24/lib/php to your php.ini include_path Installing header files: /Users/Emptyfruit/.asdf/installs/php/7.4.24/include/php/ Warning: copy(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in Command line code on line 1 Warning: copy(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in Command line code on line 1 Warning: copy(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? in Command line code on line 1 Warning: copy(https://getcomposer.org/installer): failed to open stream: No such file or directory in Command line code on line 1 Warning: hash_file(composer-setup.php): failed to open stream: No such file or directory in Command line code on line 1 Installer corrupt Warning: unlink(composer-setup.php): No such file or directory in Command line code on line 1 Could not open input file: composer-setup.php 

Источник

Оцените статью