Как отправить сообщение gcm в php без использования cURL?

Я использую следующий код для запроса HTTP POST на сервер gcm. Код всегда возвращает «Несанкционированная ошибка 401». Я читал, что речь идет о заголовках, но не может понять, что случилось. Может ли кто-нибудь сказать мне, что случилось? Есть ли другой способ отправить сообщение gcm? Любая помощь будет оценена по достоинству.

$api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0"; $message = array("msg_url" => $msg_url, "msg_title" => $msg_title); $url = 'https://android.googleapis.com/gcm/send'; $fields = array('registration_ids' => $ids,'data'=> array( "message" => $message )); $headers = array('Authorization: key=' . $api_key,'Content-Type: application/json'); // use key 'http' even if you send the request to https://... $options = array( 'http' => array( 'header'=> $headers , 'method' => 'POST', 'content' => json_encode($fields), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); var_dump($result); 

Протестировал ваш код с помощью моего ключа API. Я могу отправить запрос в GCM и получить правильный ответ. Кажется, что ваш ключ API имеет проблему

 <?php $msg_url = "http://msg.com"; $msg_title = "message_title"; $ids = array('reg_id_1', 'reg_id_2'); $api_key = "AIzaSyBhuPSdHmq6-************_qxSJr8d0"; $message = array("msg_url" => $msg_url, "msg_title" => $msg_title); $url = 'https://android.googleapis.com/gcm/send'; $fields = array('registration_ids' => $ids,'data'=> array( "message" => $message )); $headers = array('Authorization: key=' . $api_key,'Content-Type: application/json'); // use key 'http' even if you send the request to https://... $options = array( 'http' => array( 'header'=> $headers , 'method' => 'POST', 'content' => json_encode($fields), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); var_dump($result); ?>