Heim  >  Artikel  >  Backend-Entwicklung  >  php 极光推送(Push-API-v3)

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

WBOY
WBOYOriginal
2016-07-25 08:47:361222Durchsuche
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');
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php汉字转拼音(dede) Nächster Artikel:设计模式(适配器)