curl - guzzlephp 401 unauthorized issue -


i trying guzzle http post request

$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; 

where headers is

$headers = [             'authorization: key=' . $api_access_key,             'content-type: application/json',         ]; 

and post fields are

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

which response ok , expected if call request guzzlehttp client

$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(); 

it response 401 unauthorized. problem? thank you.

i assume using guzzle 6. request() method has 3 parameters, should merge 2 arrays.

something this:

$response = $client->request('post', $url, [     'form_params' => $fields,     'headers'=> [         'authorization' => 'key='.$api_access_key,         'content-type' => 'application/json' ]]); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -