使用PHP调用百度云推送

引入类库:

目录结构

push_date.php 是自己建立的,目的的做循环处理

代码如下:

<?php
require_once "sample.php";/**
 * 调用消息推送的接口
 * @param unknown $user_id
 * @param unknown $type
 * @param string $title
 * @param string $mess
 * @param string $push_type 
 */
 function push_date($user_id,$type,$title,$mess,$push_type) {    
     //$user_id 和 $type 有可能是数组
    //foreach ($user_id as $user_id) {
        if($type == 'ios') {
            test_pushMessage_ios($user_id,$mess,$push_type);
        } else if($type == 'android') {
            test_pushMessage_android($user_id,$title,$mess,$push_type);
        } else {
            test_pushMessage_ios($user_id,$mess,$push_type);
            test_pushMessage_android($user_id,$title,$mess,$push_type);
        }
     }
     //调用
     $user_id = "799497108422983611";
     $type = "android";
     $push_type = "1";
     $title = "";
     $mess = "";
     push_date($user_id,$type,$title,$mess,$push_type);
     //test_pushMessage_android($user_id);

 

sample.php 代码如下(在原始文件上改动):

<?php
require_once "./Channel.class.php";
//请开发者设置自己的apiKey与secretKey
$apiKey = "5CkwQG0FXxUGsIfhbGdgbCqz";
$secretKey = "HMsPjiHce53GM5LwkHCznYcA1CVLspGB";
//test_queryBindList('4653791');
//test_pushMessage_android('799497108422983611');
function error_output ( $str ) {    
    echo "\033[1;40;31m" . $str ."\033[0m" . "\n";
}
function right_output ( $str ) {    
    echo "\033[1;40;32m" . $str ."\033[0m" . "\n";
}
function test_queryBindList ( $userId ) { 
    global $apiKey;    
    global $secretKey;    
    $channel = new Channel ($apiKey, $secretKey);    
    $optional [ Channel::CHANNEL_ID ] = "3915728604212165383"; 
    $ret = $channel->queryBindList ( $userId, $optional ) ;    
    if ( false === $ret ) {
        error_output ( 'WRONG, ' . __FUNCTION__ . ' ERROR!!!!!' ) ;
        error_output ( 'ERROR NUMBER: ' . $channel->errno ( ) ) ;
        error_output ( 'ERROR MESSAGE: ' . $channel->errmsg ( ) ) ;
        error_output ( 'REQUEST ID: ' . $channel->getRequestId ( ) );
    } else {
        right_output ( 'SUCC, ' . __FUNCTION__ . ' OK!!!!!' ) ;
        right_output ( 'result: ' . print_r ( $ret, true ) ) ;
    }    
}
//推送android设备消息
function test_pushMessage_android ($user_id,$title,$mess,$push_type){    
    global $apiKey;    
    global $secretKey;    
    $channel = new Channel ( $apiKey, $secretKey ) ;    
    //推送消息到某个user,设置push_type = 1; 
    //推送消息到一个tag中的全部user,设置push_type = 2;
    //推送消息到该app中的全部user,设置push_type = 3;
    $push_type = $push_type; //推送单播消息
    $optional[Channel::USER_ID] = $user_id; //如果推送单播消息,需要指定user
    //optional[Channel::TAG_NAME] = "xxxx";  //如果推送tag消息,需要指定tag_name
 
    //指定发到android设备
    $optional[Channel::DEVICE_TYPE] = 3;    //指定消息类型为通知
    $optional[Channel::MESSAGE_TYPE] = 1;    //通知类型的内容必须按指定内容发送,示例如下:
    $message = '{ 
            "title": "test推送",
            "description": "推送内容",
            "notification_basic_style":7,
            "open_type":1,
            "url":"http://www.baidu.com"
         }';    
    $message_key = "msg_key";    
    $ret = $channel->pushMessage ( $push_type, $message, $message_key, $optional ) ;    
    if ( false === $ret )
    {
        error_output ( 'WRONG, ' . __FUNCTION__ . ' ERROR!!!!!' ) ;
        error_output ( 'ERROR NUMBER: ' . $channel->errno ( ) ) ;
        error_output ( 'ERROR MESSAGE: ' . $channel->errmsg ( ) ) ;
        error_output ( 'REQUEST ID: ' . $channel->getRequestId ( ) );
    }    else
    {
        right_output ( 'SUCC, ' . __FUNCTION__ . ' OK!!!!!' ) ;
        right_output ( 'result: ' . print_r ( $ret, true ) ) ;
    }
}
//推送ios设备消息
function test_pushMessage_ios ($user_id){    
    global $apiKey;    
    global $secretKey;    
    $channel = new Channel ( $apiKey, $secretKey ) ;    
    $push_type = 1; //推送单播消息
    $optional[Channel::USER_ID] = $user_id; //如果推送单播消息,需要指定user
 
    //指定发到ios设备
    $optional[Channel::DEVICE_TYPE] = 4;    //指定消息类型为通知
    $optional[Channel::MESSAGE_TYPE] = 1;    //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2.
    //旧版本曾采用不同的域名区分部署状态,仍然支持。
    $optional[Channel::DEPLOY_STATUS] = 1;    //通知类型的内容必须按指定内容发送,示例如下:
    $message = '{ 
        "aps":{
            "alert":"msg from baidu push",
            "sound":"",
            "badge":0
        }
     }';    
    $message_key = "msg_key";    
    $ret = $channel->pushMessage ( $push_type, $message, $message_key, $optional ) ;    
    if ( false === $ret )
    {
        error_output ( 'WRONG, ' . __FUNCTION__ . ' ERROR!!!!!' ) ;
        error_output ( 'ERROR NUMBER: ' . $channel->errno ( ) ) ;
        error_output ( 'ERROR MESSAGE: ' . $channel->errmsg ( ) ) ;
        error_output ( 'REQUEST ID: ' . $channel->getRequestId ( ) );
    }    else
    {
        right_output ( 'SUCC, ' . __FUNCTION__ . ' OK!!!!!' ) ;
        right_output ( 'result: ' . print_r ( $ret, true ) ) ;
    }
}
function test_initAppIoscert ( $name, $description, $release_cert, $dev_cert ){    
    global $apiKey;    
    global $secretKey;    
    $channel = new Channel ($apiKey, $secretKey) ;    
    //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2.
    //旧版本曾采用不同的域名区分部署状态,仍然支持。
    //$optional[Channel::DEPLOY_STATUS] = 1;
     
    $ret = $channel->initAppIoscert ($name, $description, $release_cert, $dev_cert) ;    
    if ( false === $ret )
    {
        error_output ( 'WRONG, ' . __FUNCTION__ . ' ERROR!!!!' ) ;
        error_output ( 'ERROR NUMBER: ' . $channel->errno ( ) ) ;
        error_output ( 'ERROR MESSAGE: ' . $channel->errmsg ( ) ) ;
        error_output ( 'REQUEST ID: ' . $channel->getRequestId ( ) );
    }    else
    {
        right_output ( 'SUCC, ' . __FUNCTION__ . ' OK!!!!!' ) ;
        right_output ( 'result: ' . print_r ( $ret, true ) ) ;
    }
}
function test_updateAppIoscert ( $name, $description, $release_cert, $dev_cert ){    
    global $apiKey;    
    global $secretKey;    
    $channel = new Channel ($apiKey, $secretKey) ;    
    //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2.
    //旧版本曾采用不同的域名区分部署状态,仍然支持。
    //$optional[Channel::DEPLOY_STATUS] = 1;
 
    $optional[ Channel::NAME ] = $name;    
    $optional[ Channel::DESCRIPTION ] = $description;    
    $optional[ Channel::RELEASE_CERT ] = $release_cert;    
    $optional[ Channel::DEV_CERT ] = $dev_cert;    
    $ret = $channel->updateAppIoscert ($optional) ;    
    if ( false === $ret )
    {
        error_output ( 'WRONG, ' . __FUNCTION__ . ' ERROR!!!!' ) ;
        error_output ( 'ERROR NUMBER: ' . $channel->errno ( ) ) ;
        error_output ( 'ERROR MESSAGE: ' . $channel->errmsg ( ) ) ;
        error_output ( 'REQUEST ID: ' . $channel->getRequestId ( ) );
    }    else
    {
        right_output ( 'SUCC, ' . __FUNCTION__ . ' OK!!!!!' ) ;
        right_output ( 'result: ' . print_r ( $ret, true ) ) ;
    }
}
function test_queryAppIoscert ( ){    
    global $apiKey;    
    global $secretKey;    
    $channel = new Channel ($apiKey, $secretKey) ;    
    //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2.
    //旧版本曾采用不同的域名区分部署状态,仍然支持。
    //$optional[Channel::DEPLOY_STATUS] = 1;
 
    $ret = $channel->queryAppIoscert () ;    
    if ( false === $ret )
    {
        error_output ( 'WRONG, ' . __FUNCTION__ . ' ERROR!!!!' ) ;
        error_output ( 'ERROR NUMBER: ' . $channel->errno ( ) ) ;
        error_output ( 'ERROR MESSAGE: ' . $channel->errmsg ( ) ) ;
        error_output ( 'REQUEST ID: ' . $channel->getRequestId ( ) );
    }    else
    {
        right_output ( 'SUCC, ' . __FUNCTION__ . ' OK!!!!!' ) ;
        right_output ( 'result: ' . print_r ( $ret, true ) ) ;
    }
}
function test_deleteAppIoscert ( ){    
    global $apiKey;    
    global $secretKey;    
    $channel = new Channel ($apiKey, $secretKey) ;    
    //如果ios应用当前部署状态为开发状态,指定DEPLOY_STATUS为1,默认是生产状态,值为2.
    //旧版本曾采用不同的域名区分部署状态,仍然支持。
    //$optional[Channel::DEPLOY_STATUS] = 1;
 
    $ret = $channel->deleteAppIoscert () ;    
    if ( false === $ret )
    {
        error_output ( 'WRONG, ' . __FUNCTION__ . ' ERROR!!!!' ) ;
        error_output ( 'ERROR NUMBER: ' . $channel->errno ( ) ) ;
        error_output ( 'ERROR MESSAGE: ' . $channel->errmsg ( ) ) ;
        error_output ( 'REQUEST ID: ' . $channel->getRequestId ( ) );
    }    else
    {
        right_output ( 'SUCC, ' . __FUNCTION__ . ' OK!!!!!' ) ;
        right_output ( 'result: ' . print_r ( $ret, true ) ) ;
    }
}

 

posted @ 2015-11-30 15:25  王永东gg  阅读(597)  评论(0)    收藏  举报