Ios推送通知PHP脚本不适用于Ubuntu 14.04

时间:2015-02-24 04:51:59

标签: php ios ubuntu

我正在使用iOS推送通知的iOS应用。我想从我的Windows PC上的PHP脚本发送通知。我使用这个PHP脚本发送通知,它也运行良好:     

// Put your device token here (without spaces):
$deviceToken = 'sdsdsdsdsczc2';
$sound = '';
// Put your private key's passphrase here:
$passphrase = 'awertf';

// Put your alert message here:
$message = 'My first push notification!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.push.apple.com:2195', $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;

// Create the payload body
$body['aps'] = array(
    'badge' => +1,
    'alert' => $message,
    'sound' => $sound

    );

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

这个php脚本适用于 windows ,但现在我正在使用 Ubuntu 14.04 操作系统,同样的php脚本给我错误。

 Message:  stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

我想这与港口2195有关。但是如何解决这个问题呢!

1 个答案:

答案 0 :(得分:0)

这是一个非常有用的链接:

http://php.net/manual/en/migration56.openssl.php http://php.net/manual/en/context.php

一份官方文档,描述了在PHP 5.6中对openssl所做的更改 学习Tou应该设置为false / true的另一个参数是有用的:例如“verify_peer_name”=> false

所以有些代码看起来像这样:

<?php
$arrContextOptions=array(
    "ssl"=>array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);  

$response = file_get_contents('ssl://'.$host.':'.$port, false, stream_context_create($arrContextOptions));

echo $response; ?>