Note : when API return responce  convert null value to empty value for avoid mobile app crashes

$json_params = array();
if (!empty($value[‘method’])) {
$params = $value[‘body’];
if (!empty($params)) {
foreach ($params as $key => $val) {
$params[$key] = addslashes_deep($val);
}
}
$obj->params = $params;
$result = $obj->$method();
//$sql = “INSERT INTO `webservices_log` (`VApiName`, `vRequestMethod`, `tRequestParams`, `tResponseParams`) VALUES (‘” . $value[‘method’] . “‘ , ‘POST’, ‘” . $post_data_string . “‘, ‘” . json_encode($result) . “‘)”;
//$sqlRes = mysql_query($sql);
} else {
/**
* Redirect to index page if method did not found in request service call.
*/
$result[‘result’] = 1;
$result[‘service_data’] = array();
$result[‘message’] = “Service Method was not specified!!!”;
if (isset($_POST[‘show_array’]) && $_POST[‘show_array’] == ‘on’) {
print_r($result);
} else {
$json_result = json_encode($result);
echo trim($json_result);
}
}

/**
* Set Display of service response either in array or json format.
*/
if ($_POST[‘show_array’] == ‘on’) {
array_walk_recursive($result, “nullToEmpty”);
$result = stripslashes_deep($result);
print_r($result);
} else {
array_walk_recursive($result, “nullToEmpty”);
$result = stripslashes_deep($result);
$json_result = json_encode($result);
echo trim($json_result);
}

/**
* Conver Null to empty
*
* @param int $value
* @return string
*/
function nullToEmpty(&$item, &$key) {
//echo $key;
if (is_null($item)) {
$item = “”;
} else
if (is_string($item)) {
//$item= utf8_decode($item);
}
}

function stripslashes_deep($value) {
if (is_array($value) || is_object($value)) {
$value = array_map(“stripslashes_deep”, (array) $value);
} else {

$value = stripslashes($value);
}
return $value;
}

function addslashes_deep($value) {
if (is_array($value) || is_object($value)) {
$value = array_map(“addslashes_deep”, (array) $value);
} else {
$value = addslashes($value);
}
return $value;
}

?>

Advertisements