Skip to content

Instantly share code, notes, and snippets.

@JustinAiken
Forked from libryder/api_class.php
Created August 7, 2012 21:34
Show Gist options
  • Save JustinAiken/3289582 to your computer and use it in GitHub Desktop.
Save JustinAiken/3289582 to your computer and use it in GitHub Desktop.
LogMyCalls API PHP Sample
<?php
/* LogMyCalls public API
*
* PHP Sample
* Author: David Ryder
* [email protected]
* http://github.com/libryder
*
* The LmcApi is a JSON wrapper using CURL to send and receive data
* in the popular and flexible JSON format.
*
* Note: The criteria array in the data array is a list of attributes related to the objects you are
* reading/writing. It doesn't contain meta data like api_key, limit, etc.
*
*/
// a sample API class to post and return JSON data
class LmcApi {
protected $endpoint, $data;
function __construct($data=null) {
$this->endpoint = 'https://api.logmycalls.com/services';
$this->data = $data;
}
function post_json($data, $action, $method = "POST") {
$formed_uri = $this->endpoint.'/'.$action;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $formed_uri);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 20);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json'));
if(isset($data)) {
$data = json_encode($data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$json_return_data = curl_exec($ch);
return $json_return_data;
}
function post_media($data=null, $action="uploadAudio", $method = "POST") {
$formed_uri = $this->endpoint.'/'.$action;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $formed_uri);
curl_setopt($ch, CURLOPT_POST, true);
if(isset($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$return_data = curl_exec($ch);
return $return_data;
}
}
?>
<?php
// This sample illustrates how to insert a call and attach the audio to the new call with two methods defined
// in api_class.php
include 'api_class.php';
$data = array (
"criteria" => array(
"ouid" => 58,
"duration" => 78,
"calldate" => "2012-02-05 14:43:22",
"caller_id" => "1234567890",
"ringto_number" => "9876543210",
"tracking_number" => "1112223333"
),
"limit" => 20,
"api_key" => "YOUR_API_KEY",
"api_secret" => "YOUR_API_SECRET",
);
// set parameters, instantiate class, and post above data to our API endpoint
// This first sample inserts a call using the above $data array
$api = new LmcApi();
$response = $api->post_json($data, "insertCall");
echo $response;
//{ "status": "success", "call_detail": { "tracking_number": "1112223333", "duration": 78, "external_id": null, "id": 193352, "is_outbound": 0, "file_url": null, "ouid": 1, "caller_id": "1234567890", "calldate": "2012-02-05 21:43:22", "status": "active", "ringto_number": "9876543210", "user_id": null } }
$json_data = json_decode($response);
// This next sample uploads the audio for our new call detail
$data = array(
"api_key" => "YOUR_API_KEY",
"api_secret" => "YOUR_API_SECRET",
"audio" => '@/var/spool/audio_file.wav',
"call_detail_id" => $json_data->call_detail->id
);
$recording = $api->post_media($data);
echo $recording;
?>
<?php
// NEW GROUP
$data = array (
"criteria" => array(
"parent_ouid" => 1,
"name" => "My Shiny Group"
),
"api_key" => "604556c893c01ee73c1e2db02a709e4e",
"api_secret" => "h71tGAajwAShA"
);
// NEW COMMENT
$data = array(
"criteria" => array(
"external_call_detail_id" => 58,
"user_id" => 1,
"comment" => "Call was insufficient."
),
"api_key" => "604556c893c01ee73c1e2db02a709e4e",
"api_secret" => "h71tGAajwAShA",
);
// NEW USER
$data = array (
"criteria" => array(
"first_name" => "Habib",
"last_name" => "Spitz",
"email" => "[email protected]",
"group_id" => 17,
"external_ouid" => 58,
"external_id" => 25
),
"api_key" => "d578f1ffd50fe2e662e9f36ca5b9ffdf",
"api_secret" => "CoccTo3xSnew2"
);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment