问题描述
在搜索有关如何发送使用GCM通知,但用PHP而不使用servlet一些信息,我发现这一点:的
我测试了这些问题的答复的工作code,还我创造了浏览器的应用程序键(与查阅情况),我给的权限此IP: .mywebsite.com / (TE PHP文件是在这个网址:http://www.mywebsite.com/~jma/cHtml5/cap/kk.php)
不过,我得到这个答复:未经授权错误401
我在做什么错了?
这是PHP文件:
< PHP
//替换从谷歌API的真实服务器API密钥
$ apiKey =虚构的钥匙;
//替换为真正的客户端注册的ID
$ registrationIDs =阵列( "APA91asdasdSDGGS232S13S4213abGqiNhCIXKjlxrkUYe_xTgTacNGB5n16b380XDd8i_9HpKGRHkvm8DDet4_WK3zumjDEKkTRWLgPS7kO-BrKzWz7eWFQaDD9PJ8zA6hlSqL9_zH21P8K22ktGKmo_VIF6YAdU9ejJovrKBTpgQktYkBZBf9Zw","APAasdasd32423dADFG91bHYYxYB7bFiX5ltbJt6A-4MBiNg7l4RS4Bqf3jIfYviaaUfZ810XJo2o66DY9-jdeJk_JR8FIZCyrmCv-eu_WLkGZ8KaoHgEDR_16H2QPm98uHpe1MjKVXbzYc4J89WMmcIrl5tHhWQnIQNzaI6Zp6yyFUNUQ");
//消息要发送
$消息=测试NotificaciónPHP;
//设置POST变量
$ URL ='https://android.googleapis.com/gcm/send';
$领域=阵列(
registration_ids'=> $ registrationIDs,
'数据'=>阵列(信息=> $消息)
);
$标题=阵列(
授权:键='。 $ apiKey,
内容类型:应用程序/ JSON
);
//打开连接
$ CH = curl_init();
//设置URL,POST数瓦尔,POST数据
curl_setopt($ CH,CURLOPT_URL,$网址);
curl_setopt($ CH,CURLOPT_POST,真正的);
curl_setopt($ CH,CURLOPT_HTTPHEADER,$头);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
// curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($域));
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,假);
// curl_setopt($ CH,CURLOPT_POST,真正的);
// curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
curl_setopt($ CH,CURLOPT_POSTFIELDS,json_en code($域));
//执行后
$结果= curl_exec($ CH);
//关闭连接
curl_close($ CH);
回声$结果;
//的print_r($结果);
//后续代码var_dump($结果);
?>
解决!
您必须使用替代浏览器的关键的关键服务器应用程序(使用IP锁定)
:)
Searching for some info about how to send notifications using GCM but with PHP instead of servlets, i found this: GCM with PHP (Google Cloud Messaging)
I tested the working code of the responses of these questions, also i created a Key for browser apps (with referers), and i give permissions to this ip: .mywebsite.com/ (te php file is on this url: "http://www.mywebsite.com/~jma/cHtml5/cap/kk.php")
But i'm getting this response: Unauthorized Error 401
What i'm doing wrong?
this is the php file:
<?php
// Replace with real server API key from Google APIs
$apiKey = "fictional key";
// Replace with real client registration IDs
$registrationIDs = array( "APA91asdasdSDGGS232S13S4213abGqiNhCIXKjlxrkUYe_xTgTacNGB5n16b380XDd8i_9HpKGRHkvm8DDet4_WK3zumjDEKkTRWLgPS7kO-BrKzWz7eWFQaDD9PJ8zA6hlSqL9_zH21P8K22ktGKmo_VIF6YAdU9ejJovrKBTpgQktYkBZBf9Zw","APAasdasd32423dADFG91bHYYxYB7bFiX5ltbJt6A-4MBiNg7l4RS4Bqf3jIfYviaaUfZ810XJo2o66DY9-jdeJk_JR8FIZCyrmCv-eu_WLkGZ8KaoHgEDR_16H2QPm98uHpe1MjKVXbzYc4J89WMmcIrl5tHhWQnIQNzaI6Zp6yyFUNUQ");
// Message to be sent
$message = "Test Notificación PHP";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $fields ));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
//print_r($result);
//var_dump($result);
?>
Solved!!!
you must use Key for server apps (with IP locking) instead of browser key
:)
这篇关于发送通知到谷歌云消息与PHP给我未经授权错误401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!