解决方案 »
- 两份PHP程序代码对比,大家认为其编程水平都怎么样?censor.class.php
- 显然这是一个url加密,我反正是没有能力,想学习
- php如何去除小数点后面的0
- php 怎么在一张图片上,挖出一个 透明的洞
- 怎么弹出个新页面
- 想扎扎的从零开始学习PHP+MYSQL,请推荐几本经典的学习资料
- 请问在win2000下 php5 + apache + mysql的具体安装步骤.......
- mysql版没人解决,求一条SQL解决下面问题
- 菜鸟问题,在线等待:提示某某不是对象,运行期错误!请问这一般是什么问题?谢谢帮忙!
- js里的!![]如何让转化成PHP
- 怎么获得函数本身有多少个参数?
- 不同浏览器下,$_SERVER数组顺序为什么不一样
<?phpclass Push
{
public $deviceToken;
public $localcert = 'ck.pem';
public $passphrase = '11111';
private function createPayload($message, $type, $sound, $number, $mid)
{
$body = array("aps" => array("alert" => $message, "badge" => 1, "sound" => 'received5.caf'));
// Encode the payload as JSON
$payload = json_encode($body); return $payload;
} // Put your private key's passphrase here:
public function pushData($message, $type, $sound, $number, $mid)
{
$pem = dirname(dirname(__FILE__)) . '/' . $this->localcert;
if (!file_exists($pem)) {
echo '沒有找到密匙文件!' . PHP_EOL;
exit;
}
//echo $pem . PHP_EOL;//debug $ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
stream_context_set_option($ctx, 'ssl', 'passphrase', $this->passphrase); // Open a connection to the APNS server
//这个为正是的发布地址
//$fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);
$serverURL = "ssl://gateway.push.apple.com:2195";
$sanBoxURL = "ssl://gateway.sandbox.push.apple.com:2195";
$fp = stream_socket_client($serverURL, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL); //echo 'Connected to APNS' . PHP_EOL; foreach ($message as $key => $value) {
$this->deviceToken = $key;
$payload = $this->createPayload($value, $type, $sound, $number, $mid); // Build the binary notification
echo 'Sendto:' . $this->deviceToken . PHP_EOL;
$msg = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $this->deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
} // Close the connection to the server
fclose($fp); if (!$result) {
return FALSE;
} else {
return TRUE;
}
}
}?>拿去用吧,之前一个项目用到的推送类
你的程序測試過可以發到push,如果發不到,請檢查pem證書和返回的狀態碼。
手机访问服务器接口时,自动查询是否有推送信息,有就读取提示,没有直接PASS。比你现在这个主动群发强,主动群发肯定知道手机已经连过服务器接口了,知道手机在线了!当然,这个只是个人意见!