title: GCM server side PHP - Unauthorized 401 Error
body: <p>I setup google developer console enabling <em>Google Cloud Messaging for Android</em>.</p>
  
  <p>In the credential side I create the browser API key typing 0.0.0.0 in the refers. Actually I create both the types of key because I found different indication in different tutorial.</p>
  
  <p><a href="http://imgur.com/m6149WQ" rel="nofollow">browser-key picture</a></p>
  
  <p><a href="http://imgur.com/5KjpDWM" rel="nofollow">server-key picture</a></p>
  
  <p>I tested the key with this PHP script</p>
  
  <pre><code>&lt;?
  /**
   * The following function will send a GCM notification using curl.
   * 
   * @param $apiKey       [string] The Browser API key string for your GCM account
   * @param $registrationIdsArray [array]  An array of registration ids to send this notification to
   * @param $messageData      [array]  An named array of data to send as the notification payload
   */
  function sendNotification( $apiKey, $registrationIdsArray, $messageData )
  {   
      $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
      $data = array(
          'data' =&gt; $messageData,
          'registration_ids' =&gt; $registrationIdsArray
      );
  
      $ch = curl_init();
  
      curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); 
      curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
      curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
  
      $response = curl_exec($ch);
      curl_close($ch);
  
      return $response;
  }
  ?&gt;
  &lt;?
  // Message to send
  $message      = "the test message";
  $tickerText   = "ticker text message";
  $contentTitle = "content title";
  $contentText  = "content body";
  
  $registrationId = '372CBFD0C4BFE728';
  $apiKey = "AIzaSyDeNN1XJBFGE_lJ_35VMUmx5cUbRCUGkjo";
  
  $response = sendNotification( 
                  $apiKey, 
                  array($registrationId), 
                  array('message' =&gt; $message, 'tickerText' =&gt; $tickerText, 'contentTitle' =&gt; $contentTitle, "contentText" =&gt; $contentText) );
  
  echo $response;    
  ?&gt;
  </code></pre>
  
  <p>I expect to obtain something like that</p>
  
  <pre><code>{"multicast_id":6782339717028231855,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
  </code></pre>
  
  <p>But I obtain (with both keys) <em>Unauthorized 401 Error</em>.</p>
  
  <p>Thanks for the help.</p>