$v){ $str .= $k.'='.$v.'&'; } $str = rtrim($str, '&'); $url = self::$push_all_url.$str; $result = self::curl_get($url, $data); return $result; } # 安卓推单个 public static function pushPartAndroid($desc){ //参数:app_key timestamp sign:sha256(appkey+timestamp+mastersecret) self::_init($desc); $auth_token = self::get_auth_token(); $data = array(); $data['target_type'] = 2; $data['target_value'] = $desc['reg_id']; $content = self::push_data_format($desc); $data['notification'] = $content; $post = array(); $post['message'] = json_encode($data); $post['auth_token'] = $auth_token; $str = '?'; foreach($post as $k=>$v){ $str .= $k.'='.$v.'&'; } $str = rtrim($str, '&'); $url = self::$push_one_url.$str; //$data['target_value'] = ''; $result = self::curl_get($url, $post); return $result; } public static function get_auth_token(){ $data = array(); $data['app_key'] = self::$appkey; $data['timestamp'] = time()*1000; $str = $data['app_key'].$data['timestamp'].self::$mastersecret; $data['sign'] = self::sha256($str); $url = self::$auth_token_url.'?app_key='.$data['app_key'].'×tamp='.$data['timestamp'].'&sign='.$data['sign']; $result = self::curl_get($url, $data); if( $result['code'] == 0 && isset($result['data']['auth_token']) ){ return $result['data']['auth_token']; }else{ return false; } } # 保存消息体 public static function save_message_content($desc,$auth_token){ $data = self::push_data_format( $desc ); $data['auth_token'] = $auth_token; $str = '?'; foreach($data as $k=>$v){ $str .= $k.'='.urlencode($v).'&'; } $str = rtrim($str, '&'); $url = self::$save_message_url.$str; $result = self::curl_get($url, $data); if( $result['code'] == 0 && isset($result['data']['message_id']) ){ return $result['data']['message_id']; }else{ return false; } } # 拼推送基本参数 public static function push_data_format($desc){ $data = array(); $time = time(); $data['app_message_id'] = $desc['app'].$time.rand(100,999); $data['title'] = $desc['title']; $data['sub_title'] = isset($desc['sub_title'])? $desc['sub_title'] : ''; $data['content'] = $desc['message']; $data['click_action_type'] = isset($desc['click_action_type'])? $desc['click_action_type'] : 0; //点击动作类型0,启动应用;1,打开应用内页(activity的intent action);2,打开网页;4,打开应用内页(activity);【非必填,默认值为0】;5,Intent scheme URL //$data['click_action_activity'] = isset($desc['click_action_activity']) ? $desc['click_action_activity'] : ''; //click_action_type为1时这里填写com.coloros.push.demo.internal click_action_type为4时这里填写com.coloros.push.demo.component.InternalActivity //$data['click_action_url'] = isset($desc['click_action_url']) ? $desc['click_action_url'] : ''; //网页地址【click_action_type为2必填,长度500】 //$data['action_parameters'] = isset($desc['action_parameters']) ? $desc['action_parameters'] : ''; //动作参数,打开应用内页或网页时传递给应用或网页【JSON格式,非必填】,字符数不能超过4K,示例:{"key1":"value1","key2":"value2"} $data['off_line_ttl'] = isset($desc['off_line_ttl'])? $desc['off_line_ttl'] : 36000; //离线消息的存活时间(time_to_live) (单位:秒), 【off_line值为true时,必填,最长3天】 $data['push_time_type'] = 0;//isset($desc['push_time_type'])? $desc['push_time_type'] : 1; //定时推送 (0, “即时”),(1, “定时”), 【只对全部用户推送生效】 $data['push_start_time'] = isset($desc['push_start_time'])? $desc['push_start_time']+1000 : $time*1000; //定时推送开始时间(根据time_zone转换成当地时间), 【push_time_type 为1必填】,时间的毫秒数 $data['show_time_type'] = 1; //展示类型 (0, “即时”),(1, “定时”) $data['show_start_time'] = isset($desc['show_start_time'])? $desc['show_start_time'] +2000: $time*1000;//定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数 $data['show_end_time'] = isset($desc['show_start_time'])? $desc['show_start_time']+ 3000: ($time+3)*1000;//定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数 $data['off_line'] = true; return $data; } /* PHP sha256() */ public static function sha256($data, $rawOutput=false){ if(!is_scalar($data)){ return false; } $data = (string)$data; $rawOutput = !!$rawOutput; return hash('sha256', $data, $rawOutput); } public static function curl_get($url, $param){ if(isset($param['auth_token']) ){ $auth_token = $param['auth_token']; }else{ $auth_token = ''; } $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token); $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_HEADER, 0);// curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 //curl_setopt($ch, CURLOPT_POSTFIELDS, $param); $data = curl_exec($ch);//运行curl curl_close($ch); return json_decode($data,true); } public static function curl_post($url, $param){ $auth_token = isset($param['auth_token'])? $param['auth_token'] : ''; unset($param['auth_token']); $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token); $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_HEADER, 0);// curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $param); $data = curl_exec($ch);//运行curl curl_close($ch); return json_decode($data,true); } }