GCM与卷曲发(PHP) [英] GCM sending with curl (php)

查看:141
本文介绍了GCM与卷曲发(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将消息发送到Android手机,但一直得到响应code 401的文字:未经授权。此外,我继续阅读使用什么键不同的故事,我所知道的3个按键:项目编号(编号),目前主要用于服务器应用程序和主要浏览器的应用程序。所以我trye​​d他们全部3,所有具有相同的结果。

我的code:

  $标题=阵列(内容类型=>中的应用程序/ JSON,授权=>中的key =的myKey。);
    $数据=阵列(
        '数据'=> $的MessageText,
        registration_ids'=>数组($ deviceRegistrationId)
    );

    $ CH = curl_init();

    curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
    curl_setopt($沟道,CURLOPT_URL,https://android.googleapis.com/gcm/send);
    curl_setopt($沟道,CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($沟道,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
    curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($数据));
    error_log中(json_en code($数据));
    $响应= curl_exec($ CH);
    curl_close($ CH);
    error_log中($响应);
 

解决方案

我改变了标题为:

  $标题=阵列(内容类型:应用/ JSON的,授权。键=的myKey);
 

和它的作品。该的myKey的关键是浏览器的应用程序。

I'm trying to send a message to an Android phone but keep getting response code 401 with text: Unauthorized. Also I keep reading different stories on what key to use, I know of 3 keys: the project ID (number), the Key for server apps and the Key for browser apps. So I have tryed them all 3, all with the same result.

My code:

$headers = array("Content-Type" => "application/json", "Authorization" => "key=" . "mykey");
    $data = array(
        'data' => $messageText,
        'registration_ids' => array($deviceRegistrationId)
    );

    $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));
    error_log(json_encode($data));
    $response = curl_exec($ch);
    curl_close($ch);
    error_log($response);

解决方案

I changed the header to:

$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . "mykey");

And it works. The mykey is Key for browser apps.

这篇关于GCM与卷曲发(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆