我最近更换了服务器,我的Google云端消息代码不再有效。我没有太多的服务器经验,我试图通过互联网寻找合适的解决方案,但一直没有成功。
我有一个GoDaddy服务器(正常工作),我已经设置并切换到linode服务器上的灯堆栈。我一定是在服务器配置中遗漏了一些东西,因为发送gcm的代码在godaddy上运行但在apache linode服务器上不起作用。
注册ID没有问题,但无法发送gcm消息。我正在使用php脚本发送它们,我知道它不是问题。
是否需要设置任何设置才能使Google Cloud Messaging正常运行?
访问日志:
###.###.###.### - - [23/Dec/2015:12:47:40 -0500] "POST /test/test.php HTTP/1.1" 500 372 "-" "Mozilla/5.0 (Macin
错误日志:
empty
我遵循了以下教程:
https://www.linode.com/docs/getting-started
https://www.linode.com/docs/websites/lamp/lamp-on-ubuntu-14-04
https://www.linode.com/docs/databases/mysql/install-mysql-phpmyadmin-on-ubuntu-12-04
https://www.linode.com/docs/security/ssl/ssl-apache2-debian-ubuntu
https://www.linode.com/docs/websites/apache/apache-web-server-ubuntu-12-04
GCM.php
<?php
class GCM {
function __construct() {
}
public function send_notification($registatoin_ids, $message, $google) {
$url = 'https://gcm-http.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . $google,
'Content-Type: application/json'
);
$ch = curl_init();
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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
echo $result;
}
}
?>
test.php的
<?php
include_once './gcm.php';
$message = $_POST['msg'];
$reg = $_POST['regid'];
$registation_ids = array();
array_push($registation_ids, $reg);
$googleAPIKey = "GOOGLE_API_KEY";
$gcm = new GCM();
$messages = array("message" => $message, "analyticsId" => "someid");
$result = $gcm->send_notification($registation_ids, $messages, $googleAPIKey);
echo $result;
?>
开启了php日志,但仍然没有。我仍然只在linode服务器上得到500内部错误。
答案 0 :(得分:0)
我将回答我自己的问题,因为我已经开始工作了。
我从未安装过PHP curl。
http://jesselau.com/2013/04/26/how-to-add-curl-to-linode-ubuntu/