Reputation: 369
In my drupal website, I use drupal web service module to provide api for android and php clients. This is my sample api link (http://localhost/myproject/api/v1/users/registered_users/retrieve.json) that return json format correctly, I have also checked the returned json in http://jsonlint.com/ that shows my json format is valid.
here is json return from localhost...
{
"status": "1",
"mobile_user": [
{
"id": "1",
"name": "saa",
"phone_no": "09978784963",
"activate_code": "",
"deposit": "0",
"created": "2015-05-29 00:00:00",
"updated": "0000-00-00 00:00:00",
"status": "1"
}
]
}
That json return works well in android, I can call webservice api and when parsing json return everything is ok without error in android. But I can't parse that json return in php, most used json decode method in php is json_decode() function. When I checked that json return with json_last_error(), "Syntax error, malformed JSON" showed. If you please, I have got to correct my code.
Thanks,
Here is my php code for calling drupal webservice....
<?php
mb_internal_encoding('UTF-8');
$url = 'http://192.168.1.111/busexpress/api/v1/mobile_user_register/mobile_user_register/retrieve.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$jsonReturn = curl_exec($ch);
curl_close($ch);
$data = stripslashes($jsonReturn);
json_decode($data);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
echo PHP_EOL;
}
?>
This is code for web service api....
<?php
function api_mobile_user_register_services_resources() {
$api = array(
'mobile_user_register' => array(
'operations' => array(
'retrieve' => array(
'help' => 'Retrieves mobile user list',
'callback' => 'mobile_user_retrieve',
'access callback' => 'user_access',
'access arguments' => array('access content'),
'access arguments append' => FALSE,
'args' => array(
array(
'name' => 'fn',
'type' => 'string',
'description' => 'Function to perform',
'source' => array('path' => '0'),
'optional' => TRUE,
'default' => '0',
),
array(
'name' => 'phone_no',
'type' => 'string',
'description' => 'get user id and activate_code by phone_no',
'source' => array('param' => 'phone_no'),
'optional' => TRUE,
'default' => '0',
),
),
),
),
),
);
return $api;
}
?>
<?php
function mobile_user_retrieve($fn,$phoneNo) {
$query = db_select('mobile_users', 'n');
$query->fields('n');
$items = $query->execute()->fetchAll();
$reply= array('status' => '1','mobile_user' => $items) ;
return $reply;
}
?>
Upvotes: 0
Views: 1631
Reputation: 596
After searching for hours, json_decode(html_entity_decode($string))
did the trick for me.
Upvotes: 0
Reputation: 369
This post solved my problem "json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK" @Kris Khairallah said we have to remove unwanted character.
This my final code...
<?php
mb_internal_encoding('UTF-8');
$url = 'http://localhost/busexpress/api/v1/mobile_user_register/mobile_user_register/retrieve.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$jsonReturn = curl_exec($ch);
curl_close($ch);
$stripresult = stripslashes(html_entity_decode($jsonReturn));
$stringLength = strlen((string)$stripresult);
for ($i = 0; $i <= 31; ++$i) {
$stripresult = str_replace(chr($i), "", $stripresult);
}
$stripresult = str_replace(chr(127), "", $stripresult);
if (0 === strpos(bin2hex($stripresult), 'efbbbf')) {
$stripresult = substr($stripresult, 3);
}
$data = json_decode($stripresult);
echo "-->". $data -> status;
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
echo PHP_EOL;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
</head>
<body>
<h2>Server async</h2>
<div><?php if($data -> status == 1){ echo "Successfully async data!"; }?></div>
</body>
</html>
Thanks
Upvotes: 1
Reputation: 1222
Delete $data = stripslashes($jsonReturn);
.
This will malform your json.
Edit:
Second use curl_setopt($ch, CURLOPT_HEADER, false);
You did not look what is in your json data and there is header status, si?
Upvotes: 0