|
<?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->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;
}
}
?>
|