Register User Device - PHP

Source Code Option 1: cURL

  
$api_endpoint = "http://send.pushnotificationapi.com/v1/";
$api_method = "device/add";

$post_fields = array(
    "api_key" => "YOUR_API_KEY",    // Your App API Key
    "device_token" => "123456",    // Device Token/Registration ID
    "platform" => "Android",    // Android, iOS or Windows
    "user_id" => 12345,    // Add this parameter only when you want to send push to only single user
    "tag" => "My New Device" // Tag your device with friendly name
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $api_endpoint . $api_method);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_fields));

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

Source Code Option 2: HttpRequest

  
<?php

$request = new HttpRequest();
$request->setUrl('https://send.pushnotificationapi.com/v1/device/add');
$request->setMethod(HTTP_METH_POST);

$request->setHeaders(array(
  'postman-token' => '60d06c5b-7f0b-ccaf-be9b-1a0f2e4e0efd',
  'cache-control' => 'no-cache',
  'content-type' => 'application/x-www-form-urlencoded'
));

$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array(
  'api_key' => 'YOUR-API-KEY',
  'device_token' => 'YOUR-DEVICE-TOKEN',
  'platform' => 'YOUR-PLATFORM',
  'user_id' => 'OPTIONAL-USER-ID',
  'tag' => 'OPTIONAL-TAG'
));

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}