Call us now on 01772 217800

JSON API Sample Code (PHP)



Tagged with
by Jonny Carter
in Code Samples

24-Sep-2015 12:13



Here is an example function for calling the JSON API with PHP

PHP Calling JSON API ------------------------------------------------------------------------------------------------------------------------------- <?php $url = 'https://textgoto.com/api/jsonapi.aspx'; // create an array in the following format $data_array = array("header" => array( "-uid"=>"[USERNAME]", "-pwd"=>"[PASSWORD]", "-action"=>"[FUNCTION]" ), "body" => array( "msisdn"=> array( "447771112222", "447123456789"), "msg"=>"[MESSAGE]", "orig"=> "[ORIGINATOR]" ) ); $data = array("campaign" => $data_array); // And then encoded as a json string $data_string = json_encode($data); $ch=curl_init($url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Content-Length: ' . strlen($data_string))); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string)); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Required for SSL curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $result = curl_exec($ch); curl_close($ch); // print out response print_r($result); ?>