Skip to content

Instantly share code, notes, and snippets.

@angebagui
Created March 25, 2017 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angebagui/3a06d9cf095ac3d43de4a154407e259e to your computer and use it in GitHub Desktop.
Save angebagui/3a06d9cf095ac3d43de4a154407e259e to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: angebagui
* Date: 03/12/2016
* Time: 13:04
*/
//namespace App\Libraries;
class AdjeminPush
{
const BASE_URL = "http://www.adjeminpush.com/api/smsmessaging/v1";
public static function send($phone, $message, $senderName){
$requestBody = array(
'receiver' => $phone,
'message' => $message,
'sender_name' => $senderName
);
$url = self::BASE_URL;
$headers = array(
'Content-Type: application/json'
);
return self::callApi($headers,$requestBody,$url,"POST",200, true);
}
/**
* Calls API Endpoints.
*
* @param array $headers An array of HTTP header fields to set
* @param array $args The data to send
* @param string $url The URL to fetch
* @param string $method Whether to do a HTTP POST or a HTTP GET
* @param int $successCode The HTTP code that will be returned on
* success
* @param bool $jsonEncodeArgs Whether or not to json_encode $args
*
* @return array Contains the results returned by the endpoint or an error
* message
*/
public static function callApi($headers,
$args,
$url,
$method,
$successCode,
$jsonEncodeArgs = false){
$ch = curl_init();
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
if (!empty($args)) {
if ($jsonEncodeArgs === true) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args));
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($args));
}
}
}else /* $method === 'GET' */{
if (!empty($args)) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($args));
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Make sure we can access the response when we execute the call
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
if ($data === false) {
return array('error' => 'API call failed with cURL error: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$response = json_decode($data, true);
$jsonErrorCode = json_last_error();
if ($jsonErrorCode !== JSON_ERROR_NONE) {
return array(
'error' => 'API response not well-formed (json error code: '
. $jsonErrorCode . ')'
);
}
if ($httpCode !== $successCode) {
$errorMessage = '';
if (!empty($response['statusMessage'])) {
$errorMessage = $response['statusMessage'];
}
return array('error' => $errorMessage);
}
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment