drupal api返回json导致语法错误,php的json_解码函数中json格式错误

drupal api返回json导致语法错误,php的json_解码函数中json格式错误,php,json,drupal-7,Php,Json,Drupal 7,在我的drupal网站中,我使用DrupalWeb服务模块为android和php客户端提供api。这是正确返回json格式的示例api链接(),我还检查了中返回的json,这表明我的json格式是有效的 下面是从localhost返回的json { "status": "1", "mobile_user": [ { "id": "1", "name": "saa", "phone_no": "09978784963", "

在我的drupal网站中,我使用DrupalWeb服务模块为android和php客户端提供api。这是正确返回json格式的示例api链接(),我还检查了中返回的json,这表明我的json格式是有效的

下面是从localhost返回的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,在android中解析json返回时,一切正常,没有错误。但我无法在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;  
}
?>

删除
$data=stripslashes($jsonReturn)

这将使您的json格式错误

编辑:

第二次使用
curl\u setopt($ch,CURLOPT\u头,false)


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

这篇文章解决了我的问题“@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> 

服务器异步

谢谢

是的,我把你的代码放在了php文件中,但还是出错了。哦,对不起,我脑子里想的是别的地方。对CULLOPT_标题使用false。如果您打印响应,您将单独获得它。