GoogleCloudMessaging PHP脚本总是返回invalidregistration [英] GoogleCloudMessaging PHP script always returning invalidregistration
问题描述
我知道有很多有相同问题的帖子,但阅读所有这些后,我无法找到问题的原因。
我写了一个GCM客户登记自己的设备从我的服务器接收邮件。这是工作,我可以存储在我的数据库中的注册ID。
我的问题是在服务器端。我用在什么地方找到谷歌搜索的脚本,但我总是收到一个错误的结果:
<$p$p><$c$c>{"multicast_id":7341760174206782539,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
PHP的code是(我使用的浏览器的API,而不是服务器API如告诉在教程中,但在尝试这两个键返回相同的消息):
&LT; PHP
//替换为来自谷歌的API真正的浏览器的API密钥
$ apiKey =AIzaSyACln4edhSZW30XcmYpqoMz_gcRCC1iFjY;
//替换为真正的客户端注册的ID
$ registrationIDs =阵列( "APA91bEdf1w4dQtsUqPT1jHhWEpvrpxzB1yrpL3RVtKrVxfzxfg2-Yl-pwHorsnmSnkqywQ8G90YcGEBoqCjgQU8CnjA0N7mOWF8bHMhHAs4ty46PPTX8yh6eSaSqvU3JTMmb-P0ma90EBG0rsQQbUh3aX895KxitI3LCiGOYqRfE5pZQ");
//消息要发送
$消息=这是一个测试;
//设置POST变量
$ URL ='https://android.googleapis.com/gcm/send';
$领域=阵列(
registration_ids'=&GT; $ registrationIDs,
'数据'=&GT;阵列(信息=&GT; $消息)
);
$标题=阵列(
授权:键='。 $ 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_exec($ CH);
//关闭连接
curl_close($ CH);
回声$结果;
?&GT;
我把真实的ID和关键,看看我做的事情。任何想法?
我发现我的问题。我的PHP库移出后某些字符或GET变量。和该characteres一个是_,因为它可以用作SQL注入。这是我的问题。修正了!
I know there are lots of posts with the same issue, but after read all of them I could not find the reason of the problem.
I wrote a GCM client to register my device to receive messages from my server. It is working and I can store the registration ID in my database.
My problem is in the server side. I'm using a script found somewhere googling, but I always receive an error result:
{"multicast_id":7341760174206782539,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
The PHP code is (I'm using BROWSER API instead of SERVER API as told on tutorial, but trying both keys returns the same message):
<?php
// Replace with real BROWSER API key from Google APIs
$apiKey = "AIzaSyACln4edhSZW30XcmYpqoMz_gcRCC1iFjY";
// Replace with real client registration IDs
$registrationIDs = array( "APA91bEdf1w4dQtsUqPT1jHhWEpvrpxzB1yrpL3RVtKrVxfzxfg2-Yl-pwHorsnmSnkqywQ8G90YcGEBoqCjgQU8CnjA0N7mOWF8bHMhHAs4ty46PPTX8yh6eSaSqvU3JTMmb-P0ma90EBG0rsQQbUh3aX895KxitI3LCiGOYqRfE5pZQ");
// Message to be sent
$message = "this is a test";
// 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 ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
?>
I put the real ids and key to see if I'm doing right. Any ideas?
I found my problem. My PHP library removed some characters from post or get variables. And one of that characteres is '_', because it can be use as SQL injection. That was my problem. Fixed!
这篇关于GoogleCloudMessaging PHP脚本总是返回invalidregistration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!