2013-01-07 17 views
9

Esiste un modo per implementare in PHP un sistema di notifica push di pubblicazione utilizzando cURL (anziché stream_socket_client)?Notifica push Apple con cURL

ho scritto:

$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); 

Ma come impostare il dispositivo token e il messaggio JSON?

+0

Perché non guardare le librerie a prontamente disponibili> http://www.easyapns.com/ – BenM

+0

@BenM perché non voglio usare la funzione stream_socket_client perché non consente l'opzione proxy con il protocollo ssl – Pierre

risposta

5

Ecco il codice per il token del dispositivo e il messaggio di JSON, penso che questo possa esserti d'aiuto.

$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

Non funziona e in base alla documentazione è necessario il formato binario non completo json quindi non dovrebbe funzionare. Sembra API dell'endpoint push di Urban Airship. – Zbyszek

+0

Non funziona. per favore aiutami a risolvere il problema –

+0

Non funziona. E non puoi inviare notifiche push ad Apple via HTTP. Devi effettivamente usare la comunicazione binaria TCP. –

4

Questo codice funziona

$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

Funziona, grazie! –

+0

Vedere http://stackoverflow.com/questions/13730351/php-https-stream-through-http-proxy se è necessario il proxy con stream_context_create. – Zbyszek

Problemi correlati