优惠券订单及其他脚本

HuaweiPush.php 4.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. class HuaweiPush{
  3. private static $appid;
  4. private static $appsecret;
  5. private static $auth_token_url = 'https://login.cloud.huawei.com/oauth2/v2/token';
  6. private static $push_url = 'https://api.push.hicloud.com/pushsend.do?nsp_ctx=';
  7. private static $package;
  8. # 安卓初始参数配置
  9. public static function _init($desc){
  10. self::$appsecret = $desc['appsecret'];
  11. self::$appid = $desc['appid'];
  12. self::$package = $desc['package'];
  13. $str = urlencode("{\"ver\":\"1\", \"appId\":\"" . self::$appid . "\"}");
  14. self::$push_url .= $str;
  15. }
  16. # 安卓推所有
  17. public static function pushAndroid($desc){
  18. echo "\n".'pushAnd'."\n";
  19. //参数:app_key timestamp sign:sha256(appkey+timestamp+mastersecret)
  20. self::_init($desc);
  21. $access_token = self::get_auth_token();
  22. if( !$access_token ) return false;
  23. $time = time();
  24. $data = array();
  25. $data['access_token'] = $access_token;
  26. $data['nsp_ts'] = $time;
  27. $data['nsp_svc'] = 'openpush.message.api.send';
  28. $data['device_token_list'] = $desc['device_token']; //JSON数值字符串,单次最多只是1000个。例如(未编码前):["12345xxxxxxxxxxxxx23456","2234567xxxxxxxxxx123456","086200503xxxxxxxxxxxxxxx300CN01"]
  29. $data['expire_time'] = date("Y-m-dTH:i", strtotime('+6 hour'));//如果用户没有在线,此消息会保存到PUSH服务器的时间。
  30. $payload = array();
  31. $payload['hps']['msg']['type'] = 3;//1 透传异步消息3 系统通知栏异步消息注意:2和4以后为保留后续扩展使用
  32. $payload['hps']['msg']['body']['content'] = $desc['message'];
  33. $payload['hps']['msg']['body']['title'] = $desc['title'];
  34. $payload['hps']['msg']['action']['type'] = 3;// 1 自定义行为:行为由参数intent定义2 打开URL:URL地址由参数url定义3 打开APP:默认值,打开App的首页
  35. //$payload['hps']['msg']['action']['param']['intent'] = '';//Action的type为1的时候表示自定义行为。
  36. //$payload['hps']['msg']['action']['param']['url'] = '';//Action的type为2的时候表示打开URL地址
  37. $payload['hps']['msg']['action']['param']['appPkgName'] = self::$package;
  38. $payload['hps']['ext']['biTag'] = isset($desc['type'])? $desc['type'] : 'Trump';
  39. //$payload['hps']['ext']['customize'] = isset($desc['ext'])? $desc['ext'] : '';
  40. //描述投递消息的JSON结构体,描述PUSH消息的:类型、内容、显示、点击动作、报表统计和扩展信息
  41. $postBody = 'access_token=' . urlencode($data['access_token']) . '&nsp_svc=' . urlencode('openpush.message.api.send') . '&nsp_ts=' . (int)urlencode($data['nsp_ts']) . '&device_token_list=' . urlencode(json_encode($data['device_token_list'])) . '&payload=' . urlencode(json_encode($payload));
  42. $result = self::curl_post(self::$push_url, $postBody);
  43. var_dump($result);
  44. return json_decode($result,true);
  45. }
  46. public static function get_auth_token(){
  47. //grant_type=client_credentials&client_secret=申请应用时获得的应用密钥&client_id=申请应用时获得的应用ID
  48. $data = array();
  49. $data['grant_type'] = 'client_credentials';
  50. $client_id = self::$appid;
  51. $client_secret = self::$appsecret;
  52. $str = 'grant_type=client_credentials&client_secret=' . $client_secret . '&client_id=' . $client_id;
  53. //$data['scope'] = 'nsp.auth nsp.user nsp.vfs nsp.ping openpush.message'; //权限列表,默认nsp.auth nsp.user nsp.vfs nsp.ping openpush.message
  54. $result = self::curl_post(self::$auth_token_url, $str);
  55. $result = json_decode($result,true);
  56. if( isset($result['access_token']) ){
  57. return $result['access_token'];
  58. }else{
  59. return false;
  60. }
  61. }
  62. public static function curl_post($url, $param){
  63. $postUrl = $url;
  64. $curlPost = $param;
  65. $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8');
  66. $ch = curl_init();//初始化curl
  67. curl_setopt($ch, CURLOPT_URL,$postUrl);
  68. curl_setopt($ch, CURLOPT_HEADER, 0);//
  69. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  71. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  72. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  73. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  74. $data = curl_exec($ch);//运行curl
  75. curl_close($ch);
  76. return $data;
  77. }
  78. }