>  기사  >  백엔드 개발  >  php 极光推送(Push-API-v3)

php 极光推送(Push-API-v3)

WBOY
WBOY원래의
2016-07-25 08:47:361222검색
php 极光推送(Push-API-v3)
                   
                               
                                       
                                       
            
  1. error_reporting(E_ALL^E_NOTICE);
  2. class ApipostAction{
  3.         private $_appkeys = 'f722b1337ded8********';
  4.         private $_masterSecret = 'bd267a37c3034fd6*******';
  5.         function request_post($url="",$param="",$header="") {
  6.                 if (empty($url) || empty($param)) {
  7.                 return false;
  8.                 }
  9.                 $postUrl = $url;
  10.                 $curlPost = $param;
  11.                 $ch = curl_init();//初始化curl
  12.                 curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
  13.                 curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
  14.                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  15.                 curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  16.                 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  17.                 curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
  18.                 // 增加 HTTP Header(头)里的字段
  19.                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  20.                 // 终止从服务端进行验证
  21.                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  22.                 $data = curl_exec($ch);//运行curl
  23.        
  24.                 curl_close($ch);
  25.                 return $data;
  26.         }
  27.         function send($title,$message)
  28.         {
  29.                 $url = 'https://api.jpush.cn/v3/push';
  30.                 $base64=base64_encode("$this->_appkeys:$this->_masterSecret");
  31.                 $header=array("Authorization:Basic $base64","Content-Type:application/json");
  32.                 // print_r($header);
  33.                 $param='{"platform":"all","audience":"all","notification" : {"alert" : "Hi,JPush!"},"message":{"msg_content":"'.$message.'","title":"'.$title.'"}}';
  34.                 $res = $this->request_post($url,$param,$header);
  35.                 $res_arr = json_decode($res, true);
  36.                  print_r($res_arr);
  37.         }
  38. }
  39. $jpush=new ApipostAction();
  40. $jpush->send('this title','this mesage');
复制代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.