27 Nov 2013

How to make REST calls in PHP

Example of calling GET request



//next example will recieve all messages for specific conversation

$service_url = 'http://example.com/api/conversations/[CONV_CODE]/messages&apikey=[API_KEY]';

$curl = curl_init($service_url);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$curl_response = curl_exec($curl);

if ($curl_response === false) {

$info = curl_getinfo($curl);

curl_close($curl);

die('error occured during curl exec. Additioanl info: ' . var_export($info));

}

curl_close($curl);

$decoded = json_decode($curl_response);

if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {

die('error occured: ' . $decoded->response->errormessage);

}

echo 'response ok!';

var_export($decoded->response);





Example of calling POST request



//next example will insert new conversation

$service_url = 'http://example.com/api/conversations';

$curl = curl_init($service_url);

$curl_post_data = array(

'message' => 'test message',

'useridentifier' => 'agent@example.com',

'department' => 'departmentId001',

'subject' => 'My first conversation',

'recipient' => 'recipient@example.com',

'apikey' => 'key001'

);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);

$curl_response = curl_exec($curl);

if ($curl_response === false) {

$info = curl_getinfo($curl);

curl_close($curl);

die('error occured during curl exec. Additioanl info: ' . var_export($info));

}

curl_close($curl);

$decoded = json_decode($curl_response);

if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {

die('error occured: ' . $decoded->response->errormessage);

}

echo 'response ok!';

var_export($decoded->response);





Example of calling PUT request



//next eample will change status of specific conversation to resolve

$service_url = 'http://example.com/api/conversations/cid123/status';

$ch = curl_init($service_url);



curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

$data = array("status" => 'R');

curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

$response = curl_exec($ch);

if ($curl_response === false) {

$info = curl_getinfo($curl);

curl_close($curl);

die('error occured during curl exec. Additioanl info: ' . var_export($info));

}

curl_close($curl);

$decoded = json_decode($curl_response);

if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {

die('error occured: ' . $decoded->response->errormessage);

}

echo 'response ok!';

var_export($decoded->response);





Example of calling DELETE request



$service_url = 'http://example.com/api/conversations/[CONVERSATION_ID]';

$ch = curl_init($service_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");

$curl_post_data = array(

'note' => 'this is spam!',

'useridentifier' => 'agent@example.com',

'apikey' => 'key001'

);

curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);

$response = curl_exec($ch);

if ($curl_response === false) {

$info = curl_getinfo($curl);

curl_close($curl);

die('error occured during curl exec. Additioanl info: ' . var_export($info));

}

curl_close($curl);

$decoded = json_decode($curl_response);

if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {

die('error occured: ' . $decoded->response->errormessage);

}

echo 'response ok!';

var_export($decoded->response);





This is some code I found on the internet. OTher option is to send the full message by using a tcp stack. Again from internet:






<?php

$str3 = "Test Data";

echo $str3;

$fp = stream_socket_client("tcp://192.168.1.26:12000", $errno, $errstr, 30);

if (!$fp)

{

echo "$errstr ($errno)<br />\n";

echo "Some problem! </br>";

}



else

{

fwrite($fp,$str3);

$str = "";



while ($str == "")

{

$str = fgets($fp, 1024);

}



fclose($fp);

echo $str;

}

?>





Receive the data:






while (($buffer = fgets($fp, 4096)) !== false) {

echo $buffer;

}

if (!feof($fp)) {

echo "Error: unexpected fgets() fail\n";

}

fclose($fp);