正在获取带有cURL的JSON响应


Getting JSON response WITH cURL

我不知道自己做错了什么,但我已经为此付出了几天的努力。

这是我从命令行发出的cURL请求:

curl -i -H "Accept: text/html" http://laravel.project/api/v1/users/4

返回:

HTTP/1.1 200 OK
Server: nginx/1.6.2
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache
Date: Sun, 29 Mar 2015 10:33:36 GMT
Set-Cookie: laravel_session=eyJpdiI6ImNPTkZIYVJZSVRKaHBOZTR3SWh0dHc9PSIsInZhbHVlIjoiblpZYVJlN2dBY1ljejNIYUQycXpsNXRWd1I5a3JSRG8wSWdDOWlHVTMrYUcrdDBNVmVuZUNkOGtJb2M4bXFpTFF3cUdoTFZOVXBWXC82Q1luSGd5bjJBPT0iLCJtYWMiOiI0ZTEwOWQxMmVhMzY2NjI1Yzc1MTBmZmRmYjUyOGQwNDlhYzRjOTNiM2FiOWIyN2E1YjA0OTM4YTUxZmNmMzMzIn0%3D; expires=Sun, 29-Mar-2015 12:33:36 GMT; Max-Age=7200; path=/; httponly
{
"data":{
  "id":4,
  "name":"Helena",
  "email":"hh@gmail.com",
  "created_at":"2015-03-26 21:13:16",
  "updated_at":"2015-03-26 21:13:16"
  }
}

所以一切看起来都很好:内容类型设置正确,响应为JSON。

但是现在看看如果我在PHP中使用带有curl的API会发生什么:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $final_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$result = curl_exec($ch);
return json_decode($result);

我得到的回复是:

{#165
  +"data": {#167
    +"id": 4
    +"name": "Helena"
    +"email": "hh@gmail.com"
    +"created_at": "2015-03-26 21:13:16"
    +"updated_at": "2015-03-26 21:13:16"
  }
}

而且,如果我返回不带json_decode$result,我得到的是:

"{
  "data":{
    "id":4,
    "name":"Helena",
    "email":"hh@gmail.com",
    "created_at":"2015-03-26 21:13:16",
    "updated_at":"2015-03-26 21:13:16"
  }
}"

正确的回答,但要加引号。我在PHP文档中读到curl_opt_returntranfer以字符串的形式返回结果,但我不是这个星球上唯一一个只想获得JSON的人。

这是我的ApiController类:

class ApiController extends Controller {
    // Base controller for API Controllers
    protected $statusCode = 200;
    protected function respond($data)
    {
        return Response::json([
           'data' => $data,
        ]);
    }
    protected function respondNotFound($message = 'No data found')
    {
        return Response::json([
            'error' => [
                'message' => $message,
                'status_code' => $this->getStatusCode(),
            ]
        ]);
    }
}

这是我的UserController:

class UserController extends ApiController {
  public function show($user)
    {
       if ($user == null)
            return $this->setStatusCode(404)->respondNotFound('User not found');
        return $this->respond($user);
    }
}

如果我返回$result而不使用json_decode,我会得到这样的结果:正确的响应,但内部引用

不,你凭什么这么想?我想问题是你是如何打印的,你很可能是用var_export($result)var_dump($result)echo json_encode($result);打印的,这是在添加引号。如果你只想要json,只需用echo $result;回显它,不需要额外的处理,只需按原样回显字符串,它已经是json了。

我认为这将解决您的问题:

curl -i -H "Accept: text/html" http://laravel.project/api/v1/users/4 | tr -d '"'
$response = (string)$result;
                    $resp_arr = explode("<!DOCTYPE",$response);
                    $obj = json_decode(trim($japi_arr[0]));
                    if(isset($obj[0]))
                    {
                    $rsp_id = $obj[0]->id;
                    $rsp_name = $obj[0]->name;