我正在尝试使用PHP运行时在appengine中使用gcm。以下是使用URLFetch服务
的代码$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的原始代码:
$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服务的代码不起作用。有人能告诉我我在哪里做错了。
答案 0 :(得分:2)
这是经过测试的代码。
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; }