【发布时间】:2013-01-02 13:06:15
【问题描述】:
我看过几篇关于如何从我的 PHP 服务器发送 GCM 消息的帖子,但我无法让它工作。这是我的代码:
public function test_gcm($id_user){
// Search user's RegIds and stores them in $regids
if(count($regids) == 0){
echo "This user has no registered device.";
return;
}
$ch = curl_init();
$data = array(
'data' => array('message'=>'my message', 'title'=>'message title'),
'registration_ids' => $regids
);
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// WRITE JSON HEADERS
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization:key=' . $apiKey)
);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
我正在使用浏览器密钥。我也尝试了服务器密钥,但它们都不起作用,curl_exec 总是返回 false。有人知道这是为什么吗?
编辑:我刚刚使用了 'netstat -tuanc |我的服务器上的 grep 173' 并执行了服务器调用。我使用 grep 173 因为如果我 ping android.googleapis.com 我 ping 这个 IP 地址。当我使用 curl_exec 时,netstat 没有显示到该 IP 地址的任何连接。这是否意味着我没有连接到 android.googleapis.com?还是我做错了?
谢谢!
【问题讨论】:
-
我得到了这样的工作:stackoverflow.com/questions/11396177/gcm-sending-with-curl-php希望它有所帮助
-
另外:我让它允许来自所有 IP 的呼叫。先这样测试一下。我也使用了浏览器密钥。
-
Yours 是我检查过的帖子之一。我修改了订单,让它和你一模一样,但它仍然没有起床。我为服务器密钥设置了“允许任何 IP”,为浏览器密钥设置了“允许任何引用者”。
-
您是否删除了 Content-type: application/json 之间的空格?我不知道这是否会有所不同。
标签: php android google-cloud-messaging