开发者

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.


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; 
 }


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜