php - "Warning: curl_setopt(): and Fatal error: " in push notification -
am trying implement push notification using php. code follows,
file 1: push.php
<?php // server file class pushnotifications { // (android)api access key google api's console. private static $api_access_key = 'aizasydg3fyaj1uw7vb-wejamjyjxio5jagasyi'; // (ios) private key's passphrase. private static $passphrase = 'joashp'; // (windows phone 8) name of our push channel. private static $channelname = "joashp"; // change above 3 vriables per app. public function __construct() { exit('init function not allowed'); } // sends push notification android users public function android($data, $reg_id) { $url = 'https://android.googleapis.com/gcm/send'; $message = array( 'title' => $data['mtitle'], 'message' => $data['mdesc'], 'subtitle' => '', 'tickertext' => '', 'msgcnt' => 1, 'vibrate' => 1 ); $headers = array( 'authorization: key=' .self::$api_access_key, 'content-type: application/json' ); $fields = array( 'registration_ids' => array($reg_id), 'data' => $message, ); return self::usecurl($url, json_encode($headers), json_encode($fields)); } // sends push's toast notification windows phone 8 users public function wp($data, $uri) { $delay = 2; $msg = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" . "<wp:notification xmlns:wp=\"wpnotification\">" . "<wp:toast>" . "<wp:text1>".htmlspecialchars($data['mtitle'])."</wp:text1>" . "<wp:text2>".htmlspecialchars($data['mdesc'])."</wp:text2>" . "</wp:toast>" . "</wp:notification>"; $sendedheaders = array( 'content-type: text/xml', 'accept: application/*', 'x-windowsphone-target: toast', "x-notificationclass: $delay" ); $response = $this->usecurl($uri, $sendedheaders, $msg); $result = array(); foreach(explode("\n", $response) $line) { $tab = explode(":", $line, 2); if (count($tab) == 2) $result[$tab[0]] = trim($tab[1]); } return $result; } // sends push notification ios users public function ios($data, $devicetoken) { $devicetoken = $devicetoken; $ctx = stream_context_create(); // ck.pem certificate file stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase); // open connection 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 connect: $err $errstr" . php_eol); // create payload body $body['aps'] = array( 'alert' => array( 'title' => $data['mtitle'], 'body' => $data['mdesc'], ), 'sound' => 'default' ); // encode payload json $payload = json_encode($body); // build binary notification $msg = chr(0) . pack('n', 32) . pack('h*', $devicetoken) . pack('n', strlen($payload)) . $payload; // send server $result = fwrite($fp, $msg, strlen($msg)); // close connection server fclose($fp); if (!$result) return 'message not delivered' . php_eol; else return 'message delivered' . php_eol; } // curl private function usecurl($url, $headers, $fields) { // open connection $ch = curl_init(); if ($url) { // set url, number of post vars, post data curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_httpheader, $headers); curl_setopt($ch, curlopt_returntransfer, true); // disabling ssl certificate support temporarly curl_setopt($ch, curlopt_ssl_verifypeer, false); if ($fields) { curl_setopt($ch, curlopt_postfields, $fields); } // execute post $result = curl_exec($ch); if ($result === false) { die('curl failed: ' . curl_error($ch)); } // close connection curl_close($ch); return $result; } } } ?>
file 2 : test.php
<?php require_once('push.php'); // message payload $msg_payload = array ( 'mtitle' => 'test push notification title', 'mdesc' => 'test push notification body', ); // android $regid = 'apa91bhdommhiro5jjrm1jvxmgqhcomcpvfdqbcpflvvaiehefi9wvrwodevvd1npz82rv2dxcyvv-omml5cjphvxnlrzkiacr99eq_irryogy7typhqdb5sg4nb8zn6rfpibuiknuwdqzr-2abv6gl_vwdzljof4w'; // ios $devicetoken = 'fe66489f304dc75b8d6e8200dff8a456e8daeacec428b427e9518741c92c6660'; // wp8 $url = 'http://s.notify.live.net/u/1/sin/hmqaaad1xjmxfq8sr0b580ncxiod6g7hiyp9ohvjjpmc2eta7u_xy_xtsah8twx7dul2azlhqoyzssq8jqrq-pqlatkw/d2luzg93c3bob25lzgvmyxvsda/ekts2gmt5bg_gb8lkdn_rg/wuhpybv02famb7tjuff7dg9aul4'; // replace above variable values pushnotifications::android($msg_payload, $regid); pushnotifications::wp8($msg_payload, $url); pushnotifications::ios($msg_payload, $devicetoken); ?>
but getting error as,
warning: curl_setopt(): must pass either object or array curlopt_httpheader argument in g:\xampp\htdocs\push_notification\pushnotifications.php on line 115 fatal error: call undefined method pushnotifications::wp8() in g:\xampp\htdocs\push_notification\testnotifications.php on line 21
what should done rectify error , push notification? pls help. thank you.
Comments
Post a Comment