Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble in using file_get_contents()

Tags:

php

I have two computers. I have configured web server on both computers and both are working. Now I want to access 1st URL from 2nd using file_get_contents().

1st URL:

http://46.7.234.111:8080/server/test_curl.php

2nd URL:

http://spicegururathgar.ie/client/test_curl.php

Code for accessing 1st URL:

$url = "http://46.7.234.111:8080/server/test_curl.php";
$url = 'http://' . str_replace('http://', '', $url); // Avoid accessing the file system
$opts = array('http' => array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$header = file_get_contents('$url', false, $context); // Not working
//$header = file_get_contents('http://wordpress.org/plugins/about/readme.txt', false, $context); // Working Fine

Apache Error Log:

[27-Dec-2013 08:31:12 UTC] PHP Warning:  file_get_contents(http://46.7.234.111:8080/server/test_curl.php) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: Connection timed out in /home/spicegur/public_html/client/test_curl.php on line 6

As I can access other files using file_get_contents() but not this one which I want. So please help me to solve this problem.

I have checked all PHP configuration on both servers but if anybody wants to check, use the following URLs:

  1. http://46.7.234.111:8080/phpinfo.php
  2. http://spicegururathgar.ie/phpinfo.php

Please ignore the file names ;)

I’m facing this problem only when I’m trying to run code from spicegururathgar.ie server, does any one know why this is happening?

Alternate try

I have also tried using PHP CURL but still I’m getting the same error. Here is my PHP CURL code. This code is working on my localhost but not on my server. Please help me with this.

$runfile = "http://46.7.234.111:8080/server/test_curl.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $runfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL,$runfile);
$content = curl_exec($ch);
curl_close($ch); 
echo $content;
like image 312
user3138552 Avatar asked Dec 01 '22 17:12

user3138552


2 Answers

$header = file_get_contents('$url',false,$context);//Not working

should be

$header = file_get_contents($url,false,$context);//Not working

There is no need for the quotes. Besides, variables aren't expanded within single quotes. If this is indeed the code you have, I can't explain how it would work ever, with any file, on either server.

like image 120
klugerama Avatar answered Dec 21 '22 19:12

klugerama


Your PHP code is correct except single quotes around $url but since Apache log contains real URL I guess it's just a typo in the question text.

The problem is that it takes too much time for spicegururathgar.ie server to load test_curl.php so basically you're getting a timeout. I've tried it with a browser waiting for a couple of minutes and test_curl.php wasn't served so it seems there's a problem with spicegururathgar.ie server.

If it is expected that it will take minutes for it to respond you can make request timout value higher:

$opts = array('http' => array(
  'header' => "User-Agent:MyAgent/1.0\r\n",
  'timeout' => 60*5 // 5 minutes
));
like image 40
Sam Dark Avatar answered Dec 21 '22 19:12

Sam Dark