2013-01-07 6 views
9

Gibt es eine Möglichkeit, in PHP ein Posting Push Notification System mit cURL (anstelle von stream_socket_client) zu implementieren?Apple Push-Benachrichtigung mit cURL

schrieb ich:

$url = 'https://gateway.sandbox.push.apple.com:2195'; 
$cert = 'AppCert.pem'; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_SSLCERT, $cert); 
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "passphrase"); 

$curl_scraped_page = curl_exec($ch); 

Aber wie das Gerät Token und die json Nachricht setzen?

+0

Warum schauen nicht leicht verfügbaren Bibliotheken> http://www.easyapns.com/ – BenM

+0

@BenM weil ich möchte stream_socket_client Funktion nicht verwenden, da es erlaubt keine Proxy-Option mit SSL-Protokoll – Pierre

Antwort

5

Hier ist der Code für Geräte-Token und Json-Nachricht, ich denke, das wird Ihnen helfen.

$url = 'https://gateway.sandbox.push.apple.com:2195'; 
$cert = 'AppCert.pem'; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_SSLCERT, $cert); 
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "passphrase"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"device_tokens": ["458e5939b2xxxxxxxxxxx3"], "aps": {"alert": "test message one!"}}'); 

$curl_scraped_page = curl_exec($ch); 
+0

Funktioniert nicht und nach der Dokumentation benötigen Sie Binärformat nicht voll json, so sollte es nicht funktionieren. Es sieht aus wie eine API des Urban Airship Push Endpunktes. – Zbyszek

+0

Es funktioniert nicht. Bitte helfen Sie mir, das zu lösen –

+0

Nicht funktioniert. Und Sie können keine Push-Benachrichtigungen per HTTP an Apple senden. Sie müssen tatsächlich TCP-Binär-Kommunikation verwenden. –

4

Dieser Code funktioniert

$deviceToken = '...'; 
$passphrase = '...'; 
$message = '...'; 
$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', 'xxx.pem'); 
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 
$body['aps'] = array('alert' => $message,'sound' => 'default'); 
$payload = json_encode($body); 
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; 
$result = fwrite($fp, $msg, strlen($msg)); 
fclose($fp); 
+0

Das funktioniert, danke! –

+0

Siehe http://stackoverflow.com/questions/13730351/php-https-stream-through-http-proxy, wenn Sie einen Proxy mit stream_context_create benötigen. – Zbyszek