user657311
user657311

Reputation: 11

Get a file from a URL

$vars='https://www.googleapis.com/latitude/v1/currentLocation?key=AIzaSyB2hw658V7RSzEYgwSHwYkaLm_505gqLhs';
$content = file_get_contents( $vars );

I am using this function for retrieving a file from a url but file_get_contents() is not working. Please suggest me a function for the same work.

Upvotes: 1

Views: 259

Answers (3)

Goran Rakic
Goran Rakic

Reputation: 1799

You should find out why file_get_contents is not working. If you are using PHP version older than 4.3, it does not exists, and can be implemented using other more low level functions.

If there is restriction on allow_url_fopen (test with phpinfo()), try the answer by ChrisR as curl will bypass this restriction. You may also use socket functions.

If there is a firewall blocking outgoing connections, you are out of luck. You will need to call your hosting support and ask about this.

At last, your server may be blocked from calling Google API.

Upvotes: 0

ChrisR
ChrisR

Reputation: 14447

file_get_contents only works if allow_url_fopen is set to "on" in your php.ini.

If it isn't you can resort to curl to get the contents of a url.

function getPage ($url) {  
     $ch = curl_init($url);
     curl_setopt($ch, CURLOPT_REFERER, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     $tmp = curl_exec ($ch);  
     curl_close ($ch);  

     return $tmp; 
 }

Upvotes: 2

Macmade
Macmade

Reputation: 53950

You should try curl or fsockopen.

  1. http://php.net/manual/en/book.curl.php
  2. http://ch2.php.net/manual/en/function.fsockopen.php

As the PHP documentation states, an URL can be used as a filename with file_get_contents() only if the fopen wrappers are enabled.

Upvotes: 0

Related Questions