使用Google Cloud Messaging可以很方便的发送消息到手机客户端。
注意:客户端连接Google服务器需要注册ID。
相关代码:
<?
$url = 'https://android.googleapis.com/gcm/send';
$serverApiKey = "YOUR API KEY AS GENERATED IN API CONSOLE";
$reg = "DEVICE REGISTRATION ID";
$data = array(
'registration_ids' => array($reg),
'data' => array('yourname' => 'Joe Bloggs')
);
print(json_encode($data));
$client = new Zend_Http_Client($url);
$client->setMethod('POST');
$client->setHeaders(array("Content-Type" => "application/json", "Authorization" => "key=" . $serverApiKey));
$client->setRawData(json_encode($data));
$request = $client->request('POST');
$body = $request->getBody();
$headers = $request->getHeaders();
print("<xmp>");
var_dump($body);
var_dump($headers);
?>