drupal api returns json causes syntax error, malformed JSON in json_decode function of php
NickName:SAWJUSTO Ask DateTime:2015-06-11T12:37:59

drupal api returns json causes syntax error, malformed JSON in json_decode function of php

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;  
}
?>

Copyright Notice:Content Author:「SAWJUSTO」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30771674/drupal-api-returns-json-causes-syntax-error-malformed-json-in-json-decode-funct

Answers
venca 2015-06-11T04:44:40

Delete $data = stripslashes($jsonReturn);.\n\nThis will malform your json.\n\nEdit:\n\nSecond use curl_setopt($ch, CURLOPT_HEADER, false);\n\nYou did not look what is in your json data and there is header status, si?",


More about “drupal api returns json causes syntax error, malformed JSON in json_decode function of php” related questions

drupal api returns json causes syntax error, malformed JSON in json_decode function of php

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)...

Show Detail

Problem with json_decode - Syntax error, malformed JSON

I'm receiving a json array from php as the return of curl_exec in PHP (first json PHP -> python, that returns another json), and decode fails due to bad syntax. The piece of API code: if($_GET['u...

Show Detail

json_decode and StackExchange api

I'm trying to use the V2.2 of StackExchange api with PHP. I'm using the Symfony project with Kriswallsmith's buzz library. The problem comes when I try to print the content of response of HTTP req...

Show Detail

PHP json_decode returns null, even though the json is valid

&lt;?php require_once('config.php'); //connection info $array = array(); // var_dump($array); $json = file_get_contents("php://input"); if (empty($json)){ //th

Show Detail

php json_decode returns NULL with syntax error

At the risk of duplication, here is my very own struggle with json_decode(). I've been through many if not all the posts on this and nothing has worked. So here goes: Here is a sample of my json

Show Detail

PHP json_decode returns null on valid string

I have the following code: $option = $this-&gt;request-&gt;post['option']; var_dump($option); echo "&lt;br&gt;"; var_dump(json_decode($option)); The dumps show: string(118) &q

Show Detail

PHP json_decode JSON_ERROR_SYNTAX

I try to load and parse JSON file from this link But I have JSON_ERROR_SYNTAX problem and Invalid argument supplied for foreach(). Why it's happen? [{ "Manufacturer": "Toyota", "Sold": 1200, "Mon..

Show Detail

I got this error Syntax error, malformed JSON

Im integrating Lightspeed API to my website like POS system. I currently getting the value file_get_contents(), but when Im trying to load it array I gives my the error 4 of json_last_error(). Her...

Show Detail

json_decode - malformed JSON from AJAX Request

I am building a request in Flash that contains a JSON encoded Array of Objects. This is encoded using as3corelib. The request is then passed to JavaScript via ExternalInterface and a jquery ajax call

Show Detail

PHP json_decode returns null value because of syntax error

$foo_json = '{"hoge":"ywwxEu|`tYdBeARGJ?nJ~BHHmDjX|PdEl@Rj@\\XVtKtK\\VbKnFrIbEzEbBnEbBfDfAxBz@LJjBl@jEzAfZzKDPY|DUdCGbAFL?|AD~AJ\\LNPJ~B|@p@Ll@FRDd@RvBnAfA`@dAl@^NhAVd@@p@?h@H`@Pv@l@TR^f@`Rd@xAFdEV...

Show Detail