PHP APNs Apple push Notification Service server-side public class

Source: Internet
Author: User
Tags pack php class strlen

A set of APNs push platform developed in the previous period is very inefficient, and through the further study of Apple's message push service, summed up a lot of experience. At the same time also referred to some of the online technology blog blog, to complete the previous written a PHP class, the code is as follows:

The code is as follows Copy Code

<?php
/**
* Applepush Apple message push public class
*/
Class Applepush
{

Const STATUS_CODE_INTERNAL_ERROR = 999;
Const ERROR_RESPONSE_SIZE = 6;
Const Error_response_command = 8;

Protected $_errorresponsemessages = Array (
0 => ' No errors encountered ',
1 => ' processing error ',
2 => ' Missing device token ',
3 => ' Missing topic ',
4 => ' Missing payload ',
5 => ' Invalid token size ',
6 => ' Invalid topic size ',
7 => ' Invalid payload size ',
8 => ' Invalid token ',
Self::status_code_internal_error => ' INTERNAL ERROR '
);

/**
* APNs Server URL
*
* @var String
*/
protected $apns _url = ' ssl://gateway.push.apple.com:2195 '; Sandbox address: ssl://gateway.sandbox.push.apple.com:2195

/**
* Push Data
*
* @var String
*/
Private $payload _json;

/**
* Data Flow objects
*
* @var Mixed
*/
Private $fp;

/**
* Set APNs Address
*
* @param string $url
*/

Public Function Setapnsurl ($url)
{
if (empty ($url)) {
return false;
} else {
$this->apns_url = $url;
}
return true;
}

/**
* Set the push message
*
* @param string $body
*/
Public Function Setbody ($body)
{
if (empty ($body)) {
return false;
} else {
$this->payload_json = Json_encode ($body);
}
return true;
}

/**
* Open APNs Server connection
*
* @param string $PEM Certificate
* @param string $passphrase certificate key
*/
Public function open ($PEM, $passphrase)
{
if (empty ($PEM)) {
return false;
}
if (empty ($passphrase)) {
return false;
}
$ctx = Stream_context_create ();
Stream_context_set_option ($ctx, ' SSL ', ' Local_cert ', $PEM);
Stream_context_set_option ($ctx, ' SSL ', ' passphrase ', $passphrase);
$fp = Stream_socket_client ($this->apns_url, $err, $errstr, Stream_client_connect, $ctx);
if (! $fp) {
return false;
}
$this-&GT;FP = $fp;
return true;
}

/**
* Send push
*
* @param string $token
*/
Public function Send ($token, $id)
{
$msg = Pack (' cnnnh* ', 1, $id, 864000, $token). Pack (' n ', strlen ($this->payload_json)). $this->payload_json;
Send it to the server
$result = fwrite ($this->fp, $msg, strlen ($msg));
return $result;
}

Public Function readerrmsg ()
{
$errInfo = @fread ($this->fp, self::error_response_size);
if ($errInfo = = False | | strlen ($errInfo)!= self::error_response_size) {
return true;
}
$errInfo = $this->parseerrmsg ($errInfo);
if (!is_array ($errInfo) | | empty ($errInfo)) {
return true;
}
if (!isset ($errInfo [' Command '], $errInfo [' StatusCode '], $errInfo [' identifier '])) {
return true;
}
if ($errInfo [' command ']!= Self::error_response_command) {
return true;
}
$errInfo [' timeline '] = time ();
$errInfo [' statusmessage '] = ' None (unknown) ';
$errInfo [' erroridentifier '] = $errInfo [' identifier '];
if (Isset ($this->_aerrorresponsemessages[$errInfo [' StatusCode ']]) {
$errInfo [' statusmessage '] = $this->_errorresponsemessages[$errInfo [' StatusCode ']];
}
return $errInfo;
}

protected function parseerrmsg ($errorMessage)
{
Return unpack (' Ccommand/cstatuscode/nidentifier ', $errorMessage);
}

/**
* Close APNs server shutdown APNs Servers connection
*
*/
Public Function Close ()
{
Close the connection to the server
Fclose ($this->FP);
return true;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.