search
Homephp教程PHP源码php 极光推送 Push API v2

php代码

<?php
	/**
		之前分享的极光推送Push-API-v3有问题(由于手机端没有对接测试不够全面)
		请大家谅解
	
	*/
 error_reporting(E_ALL^E_NOTICE);
header(&#39;Content-type: text/html;charset=UTF-8&#39;);
 
class jpush {
	private $_masterSecret = &#39;&#39;;
	private $_appkeys = &#39;&#39;;
	
	/**
	 * 构造函数
	 * @param string $username
	 * @param string $password
	 * @param string $appkeys
	 */
	function __construct($masterSecret = &#39;&#39;,$appkeys = &#39;&#39;) {
		$this->_masterSecret = $masterSecret;
		$this->_appkeys = $appkeys;
	}
	/**
	 * 模拟post进行url请求
	 * @param string $url
	 * @param string $param
	 */
	function request_post($url = &#39;&#39;, $param = &#39;&#39;) {
		if (empty($url) || empty($param)) {
			return false;
		}
		
		$postUrl = $url;
		$curlPost = $param;
		$ch = curl_init();//初始化curl
		curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
		curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
		curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
		curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
		$data = curl_exec($ch);//运行curl
		curl_close($ch);
		
		return $data;
	}
	/**
	 * 发送
	 * @param int $sendno 发送编号。由开发者自己维护,标识一次发送请求
	 * @param int $receiver_type 接收者类型。1、指定的 IMEI。此时必须指定 appKeys。2、指定的 tag。3、指定的 alias。4、 对指定 appkey 的所有用户推送消息。
	 * @param string $receiver_value 发送范围值,与 receiver_type相对应。 1、IMEI只支持一个 2、tag 支持多个,使用 "," 间隔。 3、alias 支持多个,使用 "," 间隔。 4、不需要填
	 * @param int $msg_type 发送消息的类型:1、通知 2、自定义消息
	 * @param string $msg_content 发送消息的内容。 与 msg_type 相对应的值
	 * @param string $platform 目标用户终端手机的平台类型,如: android, ios 多个请使用逗号分隔
	 */
	function send($sendno = 0,$receiver_type = 1, $receiver_value = &#39;&#39;, $msg_type = 1, $msg_content = &#39;&#39;, $platform = &#39;android,ios&#39;) {
		$url = &#39;http://api.jpush.cn:8800/sendmsg/v2/sendmsg&#39;;
		$param = &#39;&#39;;		
		$param .= &#39;&sendno=&#39;.$sendno;			
		$appkeys = $this->_appkeys;				
		$param .= &#39;&app_key=&#39;.$appkeys;		
		$param .= &#39;&receiver_type=&#39;.$receiver_type;				
		$param .= &#39;&receiver_value=&#39;.$receiver_value;		
		$masterSecret = $this->_masterSecret;		
		$verification_code = md5($sendno.$receiver_type.$receiver_value.$masterSecret);				
		$param .= &#39;&verification_code=&#39;.$verification_code;			
		$param .= &#39;&msg_type=&#39;.$msg_type;			
		$param .= &#39;&msg_content=&#39;.$msg_content;		
		$param .= &#39;&platform=&#39;.$platform;				
		$res = $this->request_post($url, $param);		
		if ($res === false) {
			return false;
		}	
		$res_arr = json_decode($res, true);	
	    $res_arr[&#39;errmsg&#39;]= "没有错误信息";
		switch (intval($res_arr[&#39;errcode&#39;])) {
			case 0:
			    $res_arr[&#39;errmsg&#39;] = &#39;发送成功&#39;;
				//echo &#39;发送成功&#39;;			    
				break;
			case 10:
			    $res_arr[&#39;errmsg&#39;] = &#39;系统内部错误&#39;;
				//echo &#39;系统内部错误&#39;;
				break;
			case 1001:
			    $res_arr[&#39;errmsg&#39;] = &#39;只支持 HTTP Post 方法,不支持 Get 方法&#39;;
				//echo &#39;只支持 HTTP Post 方法,不支持 Get 方法&#39;;
				break;
			case 1002:
				$res_arr[&#39;errmsg&#39;] = &#39;缺少了必须的参数&#39;;
				//echo &#39;缺少了必须的参数&#39;;
				break;
			case 1003:
				$res_arr[&#39;errmsg&#39;] = &#39;参数值不合法&#39;;
				//echo &#39;参数值不合法&#39;;
				break;
			case 1004:
				$res_arr[&#39;errmsg&#39;] = &#39;验证失败&#39;;
				//echo &#39;验证失败&#39;;
				break;
			case 1005:
				$res_arr[&#39;errmsg&#39;] = &#39;消息体太大&#39;;
				//echo &#39;消息体太大&#39;;
				break;
			case 1007:
				$res_arr[&#39;errmsg&#39;] = &#39;receiver_value 参数 非法&#39;;
				//echo &#39;receiver_value 参数 非法&#39;;
				break;
			case 1008:
				$res_arr[&#39;errmsg&#39;] = &#39;appkey参数非法&#39;;
				//echo &#39;appkey参数非法&#39;;
				break;
			case 1010:
				$res_arr[&#39;errmsg&#39;] = &#39;msg_content 不合法&#39;;
				//echo &#39;msg_content 不合法&#39;;
				break;
			case 1011:
				$res_arr[&#39;errmsg&#39;] = &#39;没有满足条件的推送目标&#39;;
				//echo &#39;没有满足条件的推送目标&#39;;
				break;
			case 1012:
				$res_arr[&#39;errmsg&#39;] = &#39;iOS 不支持推送自定义消息。只有 Android 支持推送自定义消息&#39;;
				//echo &#39;iOS 不支持推送自定义消息。只有 Android 支持推送自定义消息。&#39;;
				break;
			default:
				//echo &#39;调用成功&#39;;
				break;
		}		
		$msg_content = json_decode($msg_content,true);	
		$created = time();
		$created =date("Y-m-d H:i:s",$created);			
		$sql = "INSERT INTO  ".DB_NAME.".`".DB_TAB."` (`id` ,`sendno` ,`n_title` ,`n_content` ,`errcode` ,`errmsg` ,`total_user` ,`send_cnt` ,`created`)VALUES ( NULL ,&#39;".$sendno."&#39;,&#39;".$msg_content[&#39;n_title&#39;]."&#39;,&#39;".$msg_content[&#39;n_content&#39;]."&#39;, &#39;".$res_arr[&#39;errcode&#39;]."&#39;, &#39;".$res_arr[&#39;errmsg&#39;]."&#39;, &#39;&#39;,  &#39;&#39;,&#39;".$created."&#39;)";	
		$query = mysql_query($sql);		
				
		if (intval($res_arr[&#39;errcode&#39;])==0){	
				$str= "<li>第".$res_arr[&#39;sendno&#39;]."条发送".$res_arr[&#39;errmsg&#39;]."!</li>";
			}else{						
				$str= "<li>第".$res_arr[&#39;sendno&#39;]."条发送失败:".$res_arr[&#39;errmsg&#39;]."</li>";
			}
	    print_r($str);
	}
	
}


	$obj = new jpush(&#39;b1132d5*********99c28&#39;,&#39;9484b4e5*********8bce1&#39;);	
	$sendno = time();
	$receiver_value = &#39;&#39;;	
	// $msg_content = json_encode(array(&#39;n_builder_id&#39;=>0, &#39;n_title&#39;=>&#39;aaaaaaaccccc&#39;, &#39;n_content&#39;=>&#39;xxxxxxxxx&#39;));    
	$msg_content = json_encode(array(&#39;n_builder_id&#39;=>0,  &#39;n_content&#39;=>&#39;xxxxxxxxx&#39;));    
	$res = $obj->send($sendno, 4, $receiver_value, 1, $msg_content);
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version