php作为服务器向windows phone发送消息
2013-04-01 17:23
302 查看
Send Push Notifications to Windows Phone 7 from PHP
November 15, 2010 byDave
I couldn’t find a library for sending Push Notifications to Windows Phone 7 from PHP. Most samples were using an ASP.NET web service. For those of us who would like to run Push Notifications from cheaper PHP and cURL enabled hosting, I’ve created a little
helper class: (The XML may be break below, so here is the
Text Version)
class WindowsPhonePushPriority { const TileImmediately = 1; const ToastImmediately = 2; const RawImmediately = 3; const TileWait450 = 11; const ToastWait450 = 12; const RawWait450 = 13; const TileWait900 = 21; const ToastWait900 = 22; const RawWait900 = 23; } class WindowsPhonePushClient { private $device_url = ''; private $debug_mode = false; function __construct($device_url) { $this->device_url = $device_url; } public function send_raw_update($msg, $priority = WindowsPhonePushPriority::RawImmediately) { return $this->_send_push(array('X-NotificationClass: ' . $priority), $msg); } public function send_tile_update($image_url, $count, $title, $priority = WindowsPhonePushPriority::TileImmediately) { $msg = "< ?xml version=\"1.0\" encoding=\"utf-8\"?>" . "<wp :Notification xmlns:wp=\"WPNotification\">" . "</wp><wp :Tile>". "</wp><wp :BackgroundImage>" . $image_url . "</wp>" . "<wp :Count>" . $count . "</wp>" . "<wp :Title>" . $title . "</wp>" . " " . ""; return $this->_send_push(array( 'X-WindowsPhone-Target: token', 'X-NotificationClass: ' . $priority, ), $msg); } private function send_toast($title, $message, $priority = WindowsPhonePushPriority::ToastImmediately) { $msg = "< ?xml version=\"1.0\" encoding=\"utf-8\"?>" . "<wp :Notification xmlns:wp=\"WPNotification\">" . "</wp><wp :Toast>" . "</wp><wp :Text1>" . $title . "</wp>" . "<wp :Text2>" . $message . "</wp>" . "" . ""; return $this->_send_push($url, array( 'X-WindowsPhone-Target: toast', 'X-NotificationClass: ' . $priority, ), $msg); } private function _send_push($headers, $msg) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->device_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, // Add these headers to all requests $headers + array( 'Content-Type: text/xml', 'Accept: application/*' ) ); curl_setopt($ch, CURLOPT_POSTFIELDS, $msg); if ($this->debug_mode) { curl_setopt($ch, CURLOPT_VERBOSE, $this->debug_mode); curl_setopt($ch, CURLOPT_STDERR, fopen('debug.log','w')); } $output = curl_exec($ch); curl_close($ch); return array( 'X-SubscriptionStatus' => $this->_get_header_value($output, 'X-SubscriptionStatus'), 'X-NotificationStatus' => $this->_get_header_value($output, 'X-NotificationStatus'), 'X-DeviceConnectionStatus' => $this->_get_header_value($output, 'X-DeviceConnectionStatus') ); } private function _get_header_value($content, $header) { return preg_match_all("/$header: (.*)/i", $content, $match) ? $match[1][0] : ""; } }
转自【http://www.daveamenta.com/2010-11/send-push-notifications-to-windows-phone-7-from-php/】非常感谢Dave,找了很久。
相关文章推荐
- PHP用socket连接SMTP服务器发送邮件
- 如何向php服务器发送数据为json的post请求
- MailBee.NET Objects发送电子邮件(SMTP)教程一:使用SMTP服务器发送消息
- 微信实验六、微信PHP后台验证、接收和发送消息及源码下载
- Smack 结合 Openfire服务器,建立IM通信,发送聊天消息
- 微信小程序&PHP 发送模板消息通知
- PHP处理微信中带Emoji表情的消息发送和接收(Unicode字符转码编码)
- 基于mina服务器框架在android客户端接中文收不到消息以及发送不出中文的的问题
- 微信小程序 消息推送php服务器验证实例详解
- java socket 一个服务器对应多个客户端,可以互相发送消息
- php输入流php://input使用示例(php发送图片流到服务器)
- 启动 netconsole 将内核消息发送到远程 syslog 服务器
- PHP 模拟QQ登录及发送消息实现方法
- 如何在Ubuntu 16.04上安装并配置Postfix作为只发送SMTP服务器
- .net向指定服务器发送消息
- PHP实现RTX发送消息提醒
- 某种情况下造成服务器发送消息无比延迟的原因
- 公众号用户发送消息后台PHP回复没有反应的解决办法
- php之微信公众号发送模板消息