duanhe1903 2015-02-24 04:51
浏览 56
已采纳

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

I'm working on an iOS app that use iOS Push Notifications. I want to send the notification from a php script on my windows PC. I use this php script to send notification and it works well too:

// 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);

This php script works well on windows but now i'm working on Ubuntu 14.04 operating system and same php script is giving me error.

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

I guess it's something related with port 2195. But how to solve this!

  • 写回答

1条回答 默认 最新

  • drnx3715 2015-02-24 09:15
    关注

    This was an enormously helpful link to find:

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

    An official document describing the changes made to openssl in PHP 5.6 It's usefull to learn one more parameter that Tou should have set to false/true: for example "verify_peer_name"=>false

    So some code looks like this:

    <?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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改