【问题标题】:drupal api returns json causes syntax error, malformed JSON in json_decode function of phpdrupal api返回json导致语法错误,php的json_decode函数中的JSON格式错误
【发布时间】:2015-06-11 04:37:59
【问题描述】:

在我的drupal 网站中,我使用drupal web 服务模块为android 和php 客户端提供api。这是我正确返回 json 格式的示例 api 链接 (http://localhost/myproject/api/v1/users/registered_users/retrieve.json),我还检查了 http://jsonlint.com/ 中返回的 json,表明我的 json 格式有效。

这里是从本地主机返回的 json...

{
"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"
    }
  ]
}

json返回在android中运行良好,我可以调用webservice api,当解析json返回时,在android中一切正常,没有错误。但我无法解析 php 中的 json 返回,php 中最常用的 json 解码方法是 json_decode() 函数。当我使用 json_last_error() 检查 json 返回时,显示“语法错误,格式错误的 JSON”。如果你愿意,我必须更正我的代码。

谢谢,

这是我调用drupal webservice的php代码......

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

?>

这是 Web 服务 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;  
}
?>

【问题讨论】:

    标签: php json drupal-7


    【解决方案1】:

    删除$data = stripslashes($jsonReturn);

    这会使你的 json 格式不正确。

    编辑:

    第二次使用curl_setopt($ch, CURLOPT_HEADER, false);

    您没有查看您的 json 数据中的内容,并且有标头状态,si?

    【讨论】:

    • 是的,我把你的代码放在 php 文件中,但仍然出错。
    • 哦,对不起,我在想别的地方。对 CURLOPT_HEADER 使用 false。如果您打印响应,您将单独获得它。
    【解决方案2】:

    这篇文章解决了我的问题“json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK”@Kris Khairallah 说我们必须删除不需要的字符。

    这是我的最终代码...

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

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-18
      • 2020-02-19
      • 2016-12-21
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多