Reputation: 41
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:
http://46.7.234.111:8080/phpinfo.php
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;
Upvotes: 4
Views: 3512
Reputation: 844
I would advice cUrl, I was having the same problem with file_get_contents until someone adviced me cUrl. Here is a basic but working function.
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Upvotes: 2
Reputation: 2168
I just tried your code on my local server and it works fine. That means server (46.7.234.111:8080/server/test_curl.php) have no problem. So let's focus on client (spicegururathgar.ie/client/test_curl.php).
(I have to mention first) Make sure there is no any url redicects on server. Because if there is, file_get_contents cannot follow redirection. (see: CURLOPT_FOLLOWLOCATION for curl)
As far as i see, your php configuration is correct. If you can connect to 'http://wordpress.org/plugins/about/readme.txt' page, the problem may be from :8080 part. I thik you may try fsockopen.
If all of them fails, it is most probably a firewall issue.
Upvotes: 1
Reputation: 5291
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
));
Upvotes: 5
Reputation: 5703
I would also fire up Fiddler to verify exactly what your web browser is sending to the server. Some servers are configured to ignore requests from non-standard web browsers, as they customize their output greatly for each type of browser (which may reduce their potential pot of users too much).
Upvotes: 1
Reputation: 552
You should try wget http://46.7.234.111:8080/phpinfo.php
on the spicegururathgar.ie server from shell. It is possible, the firewall on the server is preventing the communication on port 8080, or ports other than 80, 433 and similar.
Even simpler test is to try and load the file on port 80.
To me this looks like a firewall issue.
Upvotes: 1
Reputation: 48751
The $url
variable on the line 6, should be wrapped within double quotes or nothing at all:
$header = file_get_contents("$url", false, $context);
OR
$header = file_get_contents($url, false, $context);
Upvotes: 2
Reputation: 3352
$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.
Upvotes: 9
Reputation: 9976
See allow_url_fopen here:
http://php.net/manual/en/filesystem.configuration.php
It's for server protection. In your case there is no security issue so it can be allowed.
Upvotes: -2