2016-08-08 68 views
0

Ich bin eine guzzle http Post-Anforderung versucht, durchguzzlephp 401 nicht autorisierte Ausgabe

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send'); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
$result = curl_exec($ch); 
curl_close($ch); 
return $result; 

wo die Header ist

$headers = [ 
      'Authorization: key=' . $api_access_key, 
      'Content-Type: application/json', 
     ]; 

und Post Felder sind

$fields = [ 
      'registration_ids' => $registrationIds, 
      'data'    => [ 
       'title' => $title, 
       'message' => $message, 
       'type' => $type, 
      ], 
     ]; 

die Antwort ok und wie erwartet aber wenn ich diese anfrage von guzzlehttp client durch

aufrufen
$URL='https://android.googleapis.com/gcm/send'; 

$client = new \GuzzleHttp\Client(['http_errors' => false]); 

$response = $client->request('POST', $URL,['form_params'=>$fields], 
           ['headers'=>[ 
            'Authorization' => 'key='.$api_access_key, 
            'Content-Type' => 'application/json']]); 

return $response->getBody()->getContents(); 

Antwort mit 401 nicht autorisiert. Wo ist mein Problem? Vielen Dank.

Antwort

0

Ich nehme an, dass Sie Guzzle 6 verwenden. request() Methode has only 3 parameters, so sollten Sie Ihre beiden Arrays zusammenführen.

Etwas wie folgt aus:

$response = $client->request('POST', $URL, [ 
    'form_params' => $fields, 
    'headers'=> [ 
     'Authorization' => 'key='.$api_access_key, 
     'Content-Type' => 'application/json' 
]]);