菜鸟教程小白 发表于 2022-12-11 17:15:48

php - 尝试通过 php 文件执行 Apple 推送通知时出错


                                            <p><p>我正在使用 php 服务器在 IOS 中处理推送通知,并且我生成了应用程序的证书和 key ,我还确保从 ssl://gateway.sandbox.push.apple.com:2196 和2195,但在尝试连接 ssl 时,我总是收到此错误,我也确信来自所有关键文件的许可</p>

<pre><code>Warning: stream_socket_client(): SSL: crypto enabling timeout in /Users/samahahmaed/Desktop/CER/newspush.php on line 24

Warning: stream_socket_client(): Failed to enable crypto in /Users/samahahmaed/Desktop/CER/newspush.php on line 24

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/samahahmaed/Desktop/CER/newspush.php on line 24
Failed to connect: 0
</code></pre>

<p>已编辑:</p>

<p>当我尝试这个命令时</p>

<pre><code>openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushCertificate.pem -key PushKey.pem -CApath /etc/ssl/certs/Entrust_Root_Certification_Authority.pem
</code></pre>

<p>我收到此错误</p>

<pre><code>CONNECTED(00000003)
write:errno=54
</code></pre>

<p>php 文件:</p>

<pre><code>&lt;?php

// Put your device token here (without spaces):
$deviceToken = &#39;mydevicetokenhere&#39;;

// Put your private key&#39;s passphrase here:
$passphrase = &#39;1234&#39;;

$message = $argv;
$url = $argv;

if (!$message || !$url)
exit(&#39;Example Usage: $php newspush.php \&#39;Breaking News!\&#39;   \&#39;https://raywenderlich.com\&#39;&#39; . &#34;\n&#34;);



$ctx = stream_context_create();
stream_context_set_option($ctx, &#39;ssl&#39;, &#39;local_cert&#39;,&#39;apple_push_notification_production.pem&#39;);
stream_context_set_option($ctx, &#39;ssl&#39;, &#39;passphrase&#39;, $passphrase);

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

if (!$fp)
exit(&#34;Failed to connect: $err $errstr&#34; . PHP_EOL);

echo &#39;Connected to APNS&#39; . PHP_EOL;

// Create the payload body
   $body[&#39;aps&#39;] = array(
   &#39;alert&#39; =&gt; $message,
   &#39;sound&#39; =&gt; &#39;default&#39;,
   &#39;link_url&#39; =&gt; $url,
);

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

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

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

if (!$result)
echo &#39;Message not delivered&#39; . PHP_EOL;
else
echo &#39;Message successfully delivered&#39; . PHP_EOL;

// Close the connection to the server
fclose($fp);
</code></pre>

<p>我对这个问题进行了很多搜索,并尝试了所有可能的解决方案,但没有任何结果我现在能做什么?</p>

<p>已编辑:</p>

<p>在 openssl 命令中添加 -debug 后最奇怪的是以下几行:
<a href="/image/0iAiR.png" rel="noreferrer noopener nofollow"><img src="/image/0iAiR.png" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><pre><code>stream_context_set_option($ctx, &#39;ssl&#39;, &#39;local_cert&#39;,&#39;apple_push_notification_production.pem&#39;);
</code></pre>

<p>此行看起来您正在使用生产证书连接到沙盒 APNS。尝试使用开发证书。您可以从苹果开发者仪表板中获得相同的信息。</p></p>
                                   
                                                <p style="font-size: 20px;">关于php - 尝试通过 php 文件执行 Apple 推送通知时出错,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38945342/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38945342/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: php - 尝试通过 php 文件执行 Apple 推送通知时出错