Urban Airship: Powering Modern Mobile
  • Ask the Community
  • Get Personal Help

Support Center

Simple PHP & API v3 examples

Last Updated: Jan 03, 2014 01:56PM PST

Urban Airship has a PHP Library which can be used to interact with API v3. However, if you're looking for some simple scripts just for sending push messages, please see below.

These scripts were tested in Terminal using PHP 5.3.26. If you are using an older version of PHP and are not able to get it to work on your terminal or server (especially if you are seeing a response code 0), please try upgrading your PHP version. If you are still unable to get it to work, you may need to modify the settings on your server. One good way to start debugging is to add "
curl_setopt($session, CURLOPT_VERBOSE, True);". Please note that we provide this code just as a sample, and we only provide full support for our official PHP library.

Here's an example of posting an iOS broadcast to API v3 using PHP and its built-in libcurl, see below. Please keep an eye out for any formatting you may need to fix.


<?php
 define('APPKEY','XXXXXXXXXXXXXXX'); // Your App Key
 define('PUSHSECRET', '
XXXXXXXXXXXXXXX'); // Your Master Secret
 define('PUSHURL', 'https://go.urbanairship.com/api/push/');

 $contents = array();
 $contents['badge'] = "+1";
 $contents['alert'] = "PHP script test";
 $contents['sound'] = "cat.caf";
 $notification = array();
 $notification['ios'] = $contents;
 $platform = array();
 array_push($platform, "ios");

 $push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);

 $json = json_encode($push);
 echo "Payload: " . $json . "\n"; //show the payload

 $session = curl_init(PUSHURL);
 curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
 curl_setopt($session, CURLOPT_POST, True);
 curl_setopt($session, CURLOPT_POSTFIELDS, $json);
 curl_setopt($session, CURLOPT_HEADER, False);
 curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
 curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
 $content = curl_exec($session);
 echo "Response: " . $content . "\n";

 // Check if any error occured
 $response = curl_getinfo($session);
 if($response['http_code'] != 202) {
     echo "Got negative response from server: " . $response['http_code'] . "\n";
 } else {

     echo "Wow, it worked!\n";
 }

 curl_close($session);
?>


Here's a broadcast example with only Android:

<?php
 define('APPKEY','
XXXXXXXXXXXXXXX'); // Your App Key
 define('PUSHSECRET', '
XXXXXXXXXXXXXXX'); // Your Master Secret
 define('PUSHURL', 'https://go.urbanairship.com/api/push/');

 $contents = array();
 $contents['alert'] = "PHP script test";
 $notification = array();
 $notification['android'] = $contents;
 $platform = array();
 array_push($platform, "android");

 $push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);

 $json = json_encode($push);
 echo "Payload: " . $json . "\n"; //show the payload

 $session = curl_init(PUSHURL);
 curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
 curl_setopt($session, CURLOPT_POST, True);
 curl_setopt($session, CURLOPT_POSTFIELDS, $json);
 curl_setopt($session, CURLOPT_HEADER, False);
 curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
 curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
 $content = curl_exec($session);
 echo "Response: " . $content . "\n";

 // Check if any error occured
 $response = curl_getinfo($session);
 if($response['http_code'] != 202) {
     echo "Got negative response from server: " . $response['http_code'] . "\n";
 } else {

     echo "Wow, it worked!\n";
 }

 curl_close($session);
?>

 

Here's another broadcast example for both iOS and Android that also includes rich push:

<?php
 define('APPKEY','
XXXXXXXXXXXXXXX'); // Your App Key
 define('PUSHSECRET', '
XXXXXXXXXXXXXXX'); // Your Master Secret
 define('PUSHURL', 'https://go.urbanairship.com/api/push/');

 $iosspecific = array();
 $iosspecific['badge'] = "+1";
 $iosspecific['sound'] = "cat.caf";
 $notification = array();
 $notification['alert'] = "PHP script test with rich push";
 $notification['ios'] = $iosspecific;
 $platform = array();
 array_push($platform, "ios");
 array_push($platform, "android"); //comment out if you don't want Android
 $richpush = array();
 $richpush['title'] = "Title of rich push";
 $richpush['body'] = "Body of rich push";

 $push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform, "message"=>$richpush);

 $json = json_encode($push);
 echo "Payload: " . $json . "\n"; //show the payload

 $session = curl_init(PUSHURL);
 curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
 curl_setopt($session, CURLOPT_POST, True);
 curl_setopt($session, CURLOPT_POSTFIELDS, $json);
 curl_setopt($session, CURLOPT_HEADER, False);
 curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
 curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
 $content = curl_exec($session);
 echo "Response: " . $content . "\n";

 // Check if any error occured
 $response = curl_getinfo($session);
 if($response['http_code'] != 202) {
     echo "Got negative response from server: " . $response['http_code'] . "\n";
 } else {

     echo "Wow, it worked!\n";
 }

 curl_close($session);
?>