PHP的GCM appengine [英] PHP GCM appengine
本文介绍了PHP的GCM appengine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在使用PHP运行时的appengine中使用gcm。以下是使用URLFetch服务的代码
I am trying to use gcm in appengine using PHP runtime. Following is the code, which uses URLFetch service
$context = array("https"=>
array( "method" => "post",
"content" => json_encode($fields),
"header" => "Content-Type: application/json\r\n" .
"Authorization: key=" . GOOGLE_API_KEY . "\r\n"
)
);
$context = stream_context_create($context);
$result = file_get_contents($url, false, $context);
以下是使用PHP Curl的原始代码:
Following is the Original code that uses PHP Curl:
$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);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
使用Curl的PHP代码运行良好,但使用appengine的urlfetch服务的代码不起作用。有人可以告诉我我在哪里做错了。
PHP code that uses Curl works well, but the code that uses urlfetch service of appengine does't work. Can someone tell me where am I doing wrong.
推荐答案
这是经过测试的代码。
This is tested code.
public function sendAndroidPushNotification($registration_ids, $message)
{
// Adding devicetoken in array
$registrationIds = array($registration_ids);
$msg = array(
'message' => $message,
'title' => 'notification center',
'tickerText' => $message,
'vibrate' => 1,
'sound' => 1,
);
$fields = array(
'registration_ids' => $registrationIds,
'data' => $msg
);
$fields = json_encode($fields);
$arrContextOptions=array(
"http" => array(
"method" => "POST",
"header" =>
'Authorization: key = '. "\r\n" .
'Content-Type: application/json'. "\r\n",
"content" => $fields,
),
"ssl"=>array(
"allow_self_signed"=>true,
"verify_peer"=>false,
),
);
$arrContextOptions = stream_context_create($arrContextOptions);
$result = file_get_contents('https://android.googleapis.com/gcm/send', false, $arrContextOptions);
return $result;
}
这篇关于PHP的GCM appengine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文