title: Google maps API key causing issue with file_get_contents() in php
body: <p>I have a pho script that takes an address and returns the lat/lon.  Works fine until I put the google api key at the end of the url. I tested the url in the browser and it works fine there. </p>
  
  <p>This works fine:</p>
  
  <pre><code>$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&amp;sensor=false');
  </code></pre>
  
  <p>This causes an error:</p>
  
  <pre><code>$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'+&amp;sensor=false+CA&amp;key=[MyAPIKeyHere]');
  </code></pre>
  
  <p>It causes the error:  failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request</p>
  
  <p>I've seen consulted similar posts such as, 
   <a href="https://stackoverflow.com/questions/12293848/warning-while-using-file-get-contents-for-google-api">Warning while using file_get_contents for google api</a> , but they weren't much help.</p>
  
  <p>Here is the complete code:</p>
  
  <pre><code>&lt;?php
  
  // Address
  $address = '1451 Broadway New York, NY 10036';
  
  // Address from command line
  // $address = readline("Enter an address: ");
  // $address = str_replace(' ','+',$address);
  
  // Get JSON results from this request
  // This url works
  $geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&amp;sensor=false');
  
  // This url creates an error
  //$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'+&amp;sensor=false+CA&amp;key=[MyAPIKeyHere]');
  
  // Convert the JSON to an array
  $geo = json_decode($geo, true);
  
  if ($geo['status'] == 'OK') {
    // Get Lat &amp; Long
    $latitude = $geo['results'][0]['geometry']['location']['lat'];
    $longitude = $geo['results'][0]['geometry']['location']['lng'];
  }
  
  // $address = str_replace('+',' ',$address);
  echo "The address ". $address. " has the following coordinates:\n" ;
  echo "Lat: ". $latitude. " Lon: ". $longitude. "\n";
  
  ?&gt;
  </code></pre>