苹果推送通知不起作用

时间:2014-08-18 作者:Meschiany

我编写了一个插件,可以向IOS设备发出苹果推送通知。

当我通过根目录手动启动它时,它工作得非常好。当我将其移动到plugin文件夹并使用以下工具将其作为插件运行时,会遇到问题:

add_action( \'publish_post\', \'post_published\' ,10,2);
它从apple服务器返回ssl错误。

代码中没有任何更改(除了include(\'wp-config.php\'),它不需要作为插件。

这是错误:

警告:stream\\u socket\\u client()[函数.stream socket client]:SSL操作失败,代码为1。OpenSSL错误消息:错误:14094410:SSL例程:SSL3\\u READ\\u字节:sslv3警报握手失败,位于/nas/wp/www/staging/geektimecoil/wp-content/plugins/wp-mes-push-to-apple/wp-mes-push-to-apple。php在线131

警告:stream\\u socket\\u client()[函数.stream socket client]:未能在/nas/wp/www/staging/geektimecoil/wp-content/plugins/wp-mes-push-to-apple/wp-mes-push-to-apple中启用加密。php在线131

警告:stream\\u socket\\u client()[函数.stream socket client]:无法连接到ssl://gateway.sandbox.push.apple.com:2195(未知错误)在/nas/wp/www/staging/geektimecoil/wp content/plugins/wp mes push to apple/wp mes push to apple中。php第131行错误:0

这是插件中的代码:

function post_published($ID, $post ) {

    // $url = "http://geektimecoil.staging.wpengine.com/googlePush.php"; global $wpdb; 
    // $post = get_post(211051);

    $categories = get_the_category($post->ID );
    if($categories){
        $cond = array();
        $cats = array();
        foreach($categories as $category) {     
            $cats[] = $category->slug;
            switch ($category->slug) {
                case "startup":
                    $cond[] = "startup";
                    break;
                case "development":
                    $cond[] = "dev";
                    break;
                case "gadgets":
                    $cond[] = "mobile";
                    break;              
                case "internet":
                    $cond[] = "internet";
                    break;
                case "hi-tech":
                    $cond[] = "hightech";
                    break;
                case "%d7%a1%d7%a7%d7%99%d7%a8%d7%95%d7%aa": // hebrew skirot
                    $cond[] = "reviews";
                    break;
                case "science":
                    $cond[] = "science";
                    break;
            }
        }
        print_r($cond);

        $condition = "( (".$cond[0]."=1)";
        for ($i=1; $i<=(count($cond)-1) ; $i++) { 
            $condition .= "or(".$cond[$i]."=1)";
        }
        $condition .= " )";
    }

    $sql="SELECT token FROM wp_mobile where device=1 AND ".$condition;
    $users = $wpdb->get_results($sql);

    print_r($sql);
    print_r($users);

        $passphrase = \'***\';

        $ctx = stream_context_create();
        stream_context_set_option($ctx, \'ssl\', \'passphrase\', $passphrase);
        stream_context_set_option($ctx, \'ssl\', \'local_cert\',\'/push/geek_dev.pem\');
        // stream_context_set_option($ctx, \'ssl\', \'local_cert\',\'/push/geek_prod.pem\');
        stream_context_set_option($ctx, \'ssl\', \'cafile\', \'/push/ios_entrust.pem\');

        # Open a connection to the APNS server
        $fp = stream_socket_client(//\'ssl://gateway.push.apple.com:2195\', $err,
                                   \'ssl://gateway.sandbox.push.apple.com:2195\', $err,
                                   $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        if (!$fp){
            echo "Error: ".$err;
            exit;
        }


        $post_url = str_replace(\'www\',\'m\',get_permalink( $post->ID));
        $body["aps"] = array(
                             "alert" => "message", //title
                             "sound" => "default",
                             "badge" => "0",
                             "url" => $post_url,
                             );

        $payload = json_encode($body);

        $msg = chr(1)                           // command (1 byte)
        . pack(\'N\', \'notification\')             // identifier (4 bytes)
        . pack(\'N\', time() + 86400)             // expire after 1 day (4 bytes)
        . pack(\'n\', 32)                         // token length (2 bytes)
        . pack(\'H*\', \'db8***b414004ccb9a***2a54d9de06\')   
        . pack(\'n\', strlen($payload))           // payload length (2 bytes)
        . $payload;                             // the JSON payload

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

        //set blocking
        stream_set_blocking($fp,0);

        //Wait to response
        sleep(1);

        fclose($fp);

        return $print;
    }


add_action( \'publish_post\', \'post_published\' ,10,2); ?>
正如我之前提到的,没有add_action 而不是作为插件,这完全相同的代码正在工作。

1 个回复
最合适的回答,由SO网友:Meschiany 整理而成

最后,当代码从插件运行时,当前工作目录设置为wp admin,而当我手动运行时,它设置为/wp content/plugins。。。

因此,调用push函数时,只需添加以下代码行:

chdir(ABSPATH . \'wp-content/plugins/*your plugins folder name*/\');

结束

相关推荐

警告:Join():在post-template.php中传递的参数无效

我在开发模式中得到了以下警告:警告:join():在/webDir/project/wp includes/post模板中传递的参数无效。php在线371在生产模式中<article> 类属性变为空(class=\"\").此错误对应于下一个函数:/** * Display the classes for the post div. * * @since 2.7.0 * * @param string|array $class One or m