天天看點

apns php推送多條,IOS APNS推送php接口

0, 'msg' => '錯誤請求' ]));}if(!isset($_GET['device_token'])){ exit(json_encode([ 'code' => 0, 'msg' => '裝置資訊缺失' ]));}if(!isset($_GET['message'])){ exit(json_encode([ 'code' => 0, 'msg' => '推送資訊缺失' ]));}if(strlen($_GET['message']) > 256){ exit(json_encode([ 'code' => 0, 'msg' => '推送資訊長度超過256個字元' ]));}if(!isset($_GET['app_id'])){ exit(json_encode([ 'code' => 0, 'msg' => 'appId缺失' ]));}$apps = [ '101' => [ 'cert' => './cert/1.pem',//ios生成的pem, 'password' => '1',//pem密碼 'debug' => '1',//開發模式 1 生産模式0 ]];if(!array_key_exists($_GET['app_id'], $apps)){ exit(json_encode([ 'code' => 0, 'msg' => 'appId錯誤' ]));}$ctx = stream_context_create();stream_context_set_option($ctx, 'ssl', 'local_cert', $apps[$_GET['app_id']]['cert']);stream_context_set_option($ctx, 'ssl', 'passphrase', $apps[$_GET['app_id']]['password']);// Open a connection to the APNS serverif($apps[$_GET['app_id']]['debug']){ $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $err_str, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);}else{ $fp = stream_socket_client( 'ssl://gateway.push.apple.com:2195', $err, $err_str, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);}if (!$fp){ exit(json_encode([ 'code' => 0, 'msg' => '連接配接APN伺服器失敗,錯誤碼:'.$err.',錯誤資訊:'.$err_str ]));}$body['aps'] = array( 'alert' => $_GET['message'], 'sound' => 'default');$payload = json_encode($body);// Build the binary notification$msg = chr(0) . pack('n', 32) . pack('H*', $_GET['device_token']) . pack('n', strlen($payload)) . $payload;// Send it to the server$result = fwrite($fp, $msg, strlen($msg));fclose($fp);if(!$result){ exit(json_encode([ 'code' => 0, 'msg' => '推送消息發送失敗,資訊:'.$result ]));}else{ exit(json_encode([ 'code' => 1, 'msg' => '推送消息發送成功,資訊:'.$result ]));}

pem生成政策見: http://my.oschina.net/u/2340880/blog/413584

本文原創釋出php中文網,轉載請注明出處,感謝您的尊重!