使用 PHP 的 GCM 实现总是面临未经授权的错误 401

我总是收到 401 错误.如果我使用的是本地服务器,请任何人都可以让我确切地知道应该是什么 HTTP 引荐来源网址.即目前我正在使用:

I am always getting 401 error.Please any one could let me know exactly what should be HTTP referrers if i am using my local server. i.e Currently I am using:

    http://localhost/GCM/index.php 

并使用它生成 API 访问密钥.

and generating a API access key using it.

这是我用于 GCM 请求的 php 代码.

Here is my php code for GCM request.

                $key='mykey';
                $headers=array('Contenttype:application/json','Authorization:key='.$key);
                $url = 'https://android.googleapis.com/gcm/send';
                $ch = curl_init();
                $curlConfig = array(
                CURLOPT_URL            => $url,
                CURLOPT_POST           => true,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS     => json_encode(array('field1' => 'some date','field2' => 'some other data',)),
                CURLOPT_HTTPHEADER     => $headers,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_SSL_VERIFYHOST => false,
                );
                curl_setopt_array($ch, $curlConfig);
                $result = curl_exec($ch);
                curl_close($ch);
                if($result==FALSE){
                    echo curl_error($ch);
                    echo curl_getinfo($ch);
                }else{
                    echo $result;
                }

推荐答案

我遇到了同样的问题.原来是 IPv6 相关的!我按照 http://developer.android.com/google/gcm/gs.html 中所述创建了服务器密钥a> 使用 0.0.0.0/0 作为允许的主机 IP,但这与 IPv6 不匹配.所以我添加了另一个允许的主机线路 IP 作为 0::0/0 并且有效(一段时间后,更新缓存配置?).

I battled same problem. Turned out it was IPv6 related! I created server key as described in http://developer.android.com/google/gcm/gs.html using 0.0.0.0/0 as permitted host IP, but that does not match IPv6. So I added another permitted host line IP as 0::0/0 and that worked (AFTER some time, to update cached config?).

将 IP 字段留空允许任何 IP 并且更容易.

Leaving the IP field blank allows ANY IP and is easier.

相关文章