欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-20 15:17:22
...
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. curl_close($ch);
  24. return $data;
  25. }
  26. function send($title,$message)
  27. {
  28. $url = 'https://api.jpush.cn/v3/push';
  29. $base64=base64_encode("$this->_appkeys:$this->_masterSecret");
  30. $header=array("Authorization:Basic $base64","Content-Type:application/json");
  31. // print_r($header);
  32. $param='{"platform":"all","audience":"all","notification" : {"alert" : "Hi,JPush!"},"message":{"msg_content":"'.$message.'","title":"'.$title.'"}}';
  33. $res = $this->request_post($url,$param,$header);
  34. $res_arr = json_decode($res, true);
  35. print_r($res_arr);
  36. }
  37. }
  38. $jpush=new ApipostAction();
  39. $jpush->send('this title','this mesage');
复制代码