title: Using stream_context_create to do a POST -- Result is false:(
body: <p>Maybe someone can point out what I may be doing wrong here. I'm messing around with the Google API and OAuth so I can register users through google to my application. I found the PHP they provide pretty cumbersome so I decided to get more practice performing https requests and such. I've had some success so far, but the final step in getting a token to exchange for user information requires a POST method. Simply redirecting the browser, copying the final url with a GET method returns an error. I'm about to start digging around in the cURL extension next, but maybe someone can spot whats wrong with this code?</p>
  
  <pre><code>$url = "https://accounts.google.com/o/oauth2/token";
  $fields = array(
      'code' =&gt; $_GET['code'],
      'client_id' =&gt; $google['clientID'],
      'client_secret' =&gt; $google['clientSecret'],
      'redirect_uri' =&gt; "http://www.qwiku.com/scripts/php/google/reg_response.php",
      'grant_type' =&gt; "authorization_code"
  );
  
  $data = http_build_query($fields);
  
  echo $data."&lt;br /&gt;";
  
  $context = stream_context_create(array(
      'http' =&gt; array(
          'method' =&gt; 'POST',
          'header' =&gt; 'Content-Type: application/x-www-form-urlencoded',
          'content' =&gt; $data
      )
  ));
  
  $result = file_get_contents($url, false, $context);
  
  var_dump($result);  
  </code></pre>
  
  <p>The dump of result is false. You can see the google documentation here but I'm 99% sure the data is formulated correctly. <a href="http://code.google.com/apis/accounts/docs/OAuth2WebServer.html#formingtheurl">http://code.google.com/apis/accounts/docs/OAuth2WebServer.html#formingtheurl</a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I'm now using curl and it appears to be working. Only problem is google is returning an error of "invalid_grant." Not sure why as it's set to exactly what they specify.</p>
  
  <pre><code>$url = 'https://accounts.google.com/o/oauth2/token';
  $ch = curl_init($url);
  
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  
  $response = curl_exec($ch);
  curl_close($ch);
  </code></pre>
  
  <p><strong>Update 2</strong></p>
  
  <p>I refreshed the whole process as I was just refreshing the redirect and bingo, it's working. Code must have expired.</p>