Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php file_get_contents and &

Tags:

I'm trying to use php's file_get_content('a url');

The thing is if the url has '&' in it, for example

file_get_contents('http://www.google.com/?var1=1&var2=2')

it automatically make a requests to www.google.com/?var1=1&var2=2

How do I prevent that from happening?

like image 817
Patrick Avatar asked Apr 17 '10 02:04

Patrick


People also ask

What is the difference between file_get_contents () function and file () function?

The file_get_contents() function reads entire file into a string. The file() function reads the entire file in a array, whereas file_get_contents() function reads the entire file into a string.

What is use of file_get_contents in PHP?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

Which is faster cURL or file_get_contents?

file_get_contents() is slightly faster than cURL.


1 Answers

I agree with the original poster of the question. To be very specific:

http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=301+E.+Linwood+Avenue++Turlock%2C+CA

This requires the sensor=false variable to be passed, or the query will return BAD results from Google. If I pass this STRING through file_get_contents, it (PHP file_get_contents) replaces the "&" with "&" so Google doesn't like me:

Array (     [type] => 2     [message] => file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=301 E. Linwood Avenue  Turlock, CA&amp;sensor=false) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request ) 

So this is the solution I came up with, using http_build_query

$myURL = 'http://maps.googleapis.com/maps/api/geocode/json?';            $options = array("address"=>$myAddress,"sensor"=>"false");     $myURL .= http_build_query($options,'','&');      $myData = file_get_contents($myURL) or die(print_r(error_get_last())); 

I also include the code (thanks Marco K.) I found on the PHP website to use a custom function for PHP < 5:

if (!function_exists('http_build_query')) {      function http_build_query($data, $prefix='', $sep='', $key='') {          $ret = array();          foreach ((array)$data as $k => $v) {              if (is_int($k) && $prefix != null) {                  $k = urlencode($prefix . $k);              }              if ((!empty($key)) || ($key === 0))  $k = $key.'['.urlencode($k).']';              if (is_array($v) || is_object($v)) {                  array_push($ret, http_build_query($v, '', $sep, $k));              } else {                  array_push($ret, $k.'='.urlencode($v));              }          }          if (empty($sep)) $sep = ini_get('arg_separator.output');          return implode($sep, $ret);      }// http_build_query  }//if 
like image 149
mshaffer Avatar answered Sep 23 '22 15:09

mshaffer