PHP iOS推送通知SSL错误

时间:2016-04-12 12:47:02

标签: php ios ssl push-notification apns-php

我发现了堆栈溢出,谷歌中有很多问题在哪里问这个我无法找到解决办法...... 这是我的代码:

<?php
$db_host = "localhost";
$db_db = "";
$db_user = "";
$db_pass = "";

mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_db);

if($_POST['message']){

    if($_POST['city'] == "ALL"){
        $query = mysql_query("SELECT * FROM user_details WHERE devicetoken != ''");
        $resultMessage = "Notifications sent to all users!";
    } else {
        $query = mysql_query("SELECT * FROM user_details WHERE city = '".$_POST['city']."' AND devicetoken != ''");
        $resultMessage = "Notifications sent to users in ".$_POST['city']."!";
    }

    $deviceToken = ARRAY();
    while ($row = mysql_fetch_array($query)) {
        $deviceToken[] = $row["devicetoken"];
    }



    $message = stripslashes($_POST['message']);


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

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


    $ssl='ck.pem';


    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', $ssl);
    stream_context_set_option($ctx, 'ssl', 'passphrase', '');
    $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    if(!$fp){
        print "Failed to connect $err $errstrn";
        return;
    } else {
        print $resultMessage;
    }


    $apple_expiry = time() + (90 * 24 * 60 * 60);

    $devArray = array();
    $devArray[] = $deviceToken;
    foreach($deviceToken as $token){
        // Build the binary notification

        $token = str_replace('(', '', $token);
        $token = str_replace(')', '', $token);
        $token = str_replace('Optional', '', $token);

        $msg.= chr(0) . pack('n', 32) . pack('H*',trim($token)) .  pack("N', $apple_expiry) . pack('n', strlen($payload)) . $payload;

        print "<br>Message sent to token ";
        print $token;


        fwrite($fp, $msg, strlen($msg));

    }
    fclose($fp);
} else {

    echo "<form action='' method='post'>";
    echo "<input type='text' name='message' maxlength='100'>";
    echo "<select name='city'>";
    echo "<option value='ALL'>ALL</option>";

    $query = mysql_query("SELECT * FROM city");
    while ($row = mysql_fetch_array($query)) {
        echo "<option value='".$row['city_name']."'>".$row['city_name']."</option>";
    }

    echo "</select>";
    echo "<input type='submit' value='Send Notification'>";
    echo "</form>";

}
?>

它每次都给我这个错误:

  

警告:fwrite():SSL操作失败,代码为1. OpenSSL错误消息:错误:1409F07F:SSL例程:SSL3_WRITE_PENDING:错误的写入重试

0 个答案:

没有答案