0

I am working on a website, that will have a large number of files. So, I made a separate server for my files such as images and txt files. The problem is that php's file_get_contents function does not work for this server.

I have tried echo file_get_contents("http://url"); and I get nothing, but when I do echo file_get_contents("http://google.com"); I get google's homepage. This the same case for a curl connection.

$ch = curl_init();
$url = "http://running-files.rf.gd/hello.html";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$body = curl_exec($ch);
$info  = curl_getinfo($ch); 
$error = curl_errno($ch);  
curl_close($ch);  

echo $body;

My guess is that there is something need in the .htaccess file. Anyone have some suggestions?

Pete
  • 333
  • 1
  • 12
  • So using curl also fails to get a response? Use curl_getinfo to pull all the information from the connection like status codes. – Devon Bessemer Feb 09 '18 at 17:45
  • @Pete Do you mind if I ask for you to specify the url? – Lakshitha Kanchana Feb 09 '18 at 17:48
  • http://running-files.rf.gd but it redirects to main server – Pete Feb 09 '18 at 17:49
  • file_get_contents() should follow redirects by default, but curl will not. You need to specify `curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);` – Devon Bessemer Feb 09 '18 at 17:51
  • I need to get a file such as http://running-files.rf.gd/Trails/cf3fe666-082a-11e8-81bc-feed01140002-1.gpx – Pete Feb 09 '18 at 17:56
  • Basic debugging : look at the source code of your page, your variable $body is not empty. You need to allow direct access to the files you want to download. For now, the url you gave as example sends back a html response containing JavaScript. The javascript makes the redirection to the file to download. Browsers do interpret JavaScript, file_get_contents or curl do not. – Zimmi Feb 09 '18 at 18:36

1 Answers1

-1

If you're opening a URI with special characters, (such as spaces) you need to encode the URI with urlencode().

Ex:

file_get_contents("http://domain-name.com?id=".urlencode("something with special characters"));

"something with special characters" can be a variable at most cases

Sammitch
  • 30,782
  • 7
  • 50
  • 77
Lakshitha Kanchana
  • 844
  • 2
  • 12
  • 32
  • 4
    Not really an answer since nothing in the question implies they're using special characters. And urlencode is supposed to be used on individual values, not the entire URL. – Devon Bessemer Feb 09 '18 at 17:46