我正在尝试使用 php 向 gcm 发送请求
我发现这篇文章 GCM with PHP (Google Cloud Messaging)
我的代码:
public function gcmSend($registrationIdsArray, $messageData) {
// Replace with real BROWSER API key from Google APIs
$apiKey = "my key";
// Replace with real client registration IDs
$registrationIDs = $registrationIdsArray;
// Message to be sent
$message = $messageData;
// 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);
return $result;
}
它总是返回:
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
对于 api 密钥,我尝试了服务器密钥和浏览器密钥
对于服务器密钥,我设置了 $_SERVER['SERVER_ADDR']; 的结果
如果我的请求是http://www.api.mysite.com/test我设置的浏览器密钥
*.mysite.com/*
密钥可能有激活时间?
我的服务器是互惠服务器,IP可以被列入黑名单吗?
谢谢