Home  >  Article  >  Backend Development  >  百度PUSH PHP服务器端

百度PUSH PHP服务器端

WBOY
WBOYOriginal
2016-06-23 13:30:511282browse

分享是一种美德!

【功能】:百度push 推送,php服务器端,版本为3.0

首先下载百度官方提供的demo,地址:http://push.baidu.com/sdk/push_server_sdk_for_php
api文档地址:http://push.baidu.com/doc/php/api

以下代码例子采用的是tp框架

import("Vendor.BaiduPushService.sdk");        // 创建SDK对象.        $sdk = new \PushSDK();        // 设置消息类型为 通知类型.        $opts = array(            'msg_type' => 1        );        $data = $this->getPushData();        show_bug($data);        foreach ($data as $val) {            $message = array(                'title' => $val['title'], // 消息的标题                'description' => $val['content'],// 消息内容                 'custom_content'=>array(                    'member_id'=>$val['member_id'],                    'member_phone'=>$val['member_phone'],//用户手机号                )            );            $channelId = $val['channel_id'];            // 向目标设备发送一条消息            $rs = $sdk->pushMsgToSingleDevice($channelId, $message, $opts);            if ($rs === false) {                $map = array(                    "error_code" => $sdk->getLastErrorCode(),                    "error_msg" => $sdk->getLastErrorMsg(),                );                $this->Information->where(array("id" => $val['id']))->save($map);            } else {                $this->updateInformation($val['id'], $rs);                // 将打印出消息的id,发送时间等相关信息.                // print_r($rs);            }

【注】
1. apikey 和secretkey需要在百度控制台中获取,具体操作看百度api文档;
2. channelId 需要在客户获取;
3. 自定义字段可以用custom_content通过array传递;

版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn