2

我正在为推送通知开发 iPhone 应用程序,当我在本地主机上运行 php 文件时出现错误。我已经正确创建了证书,.pem 文件。然后当我在本地主机上测试应用程序时,会显示警告。


警告:stream_socket_client() [function.stream-socket-client]:无法在 /Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush 中设置私钥文件 `/Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/ck.pem' .php 在第 21 行

警告:stream_socket_client() [function.stream-socket-client]:未能在第 21 行的 /Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php 中创建 SSL 句柄

警告:stream_socket_client() [function.stream-socket-client]:无法在第 21 行的 /Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/simplepush.php 中启用加密

警告:stream_socket_client() [function.stream-socket-client]:无法连接到 /Applications/XAMPP/xamppfiles/htdocs/PushNotificationProject/ 中的 ssl://gateway.sandbox.push.apple.com:2195(未知错误)第 21 行的 simplepush.php 连接失败:0


我收到这个错误。这个错误的原因可能是什么?

这是我的 php 代码。

<?php
// Put your device token here (without spaces):
$deviceToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// Put your private key's passphrase here:
$passphrase = 'xxxxxxxxxx';

// 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.sandbox.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(
    'alert' => $message,
    'sound' => 'default',
    'badge' => '1'
);

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

请帮我解决这个错误。

4

3 回答 3

0

I have seen that you didn't use any SSL Certificate. Create you SSL Certificate for your app first.

enter image description here

于 2013-08-05T17:36:35.920 回答
0

我有同样的问题,它已经解决了。

确保您的 $passphrase 是正确的。$passphrase 是您的 .pem 文件的密码

如果可能,您应该再次创建 .pem。

祝你好运!

于 2013-10-03T09:51:40.693 回答
-1
I'm Ravi Malviya.

Code snippets.

//*//Notification Configration.(Can Only Test On Device)

1.PushChatKey.certSigningRequest //Computer authentication certificate then from keychain export PushChatKey.p12
2.Explicit App ID //create it With Enable push notifcation. 
3.aps_development.cer //create it under app id and then download it.
4.Create developer certificate and provisioning profile on This App id

$ openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem / /convert Apple Development iOS Push Services  to .pem (public key)
$ openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12 // convert PushChatKey.p12 to .pem (public key)

$ cat PushChatCert.pem PushChatKey.pem > ck.pem //concate pub and pri key //RSA

$ telnet gateway.sandbox.push.apple.com 2195 //check properly APNS working or not

$ openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem //Enter pass phrase for PushChatKey.pem:

//Web server side Configration.
1.sudo su - (create root login from login options->network Account server (click join)->open directory utility ->Edit->Enable root)
2.Start Apache
# apachectl start
3.Enter in browser http://localhost
4.Enable Php Source can get under this directory
# cd /etc/apache2/
# cp httpd.conf httpd.conf.bak
5.open httpd.conf in textMate
vi httpd.conf
6.remove # and save file from 
LoadModule php5_module libexec/apache2/libphp5.so
7.Restart your apache
# apachectl restart
Varify php enable or not place php file in /Library/WebServer/Documents
Open url: http://localhost/fileName.php

//////From Command line
cd Documents/RAndD/ZObjectiveC/
php simplepush.php
于 2015-08-08T13:12:17.823 回答