简体   繁体   English

使用 cURL 获取 JSON 响应

[英]Getting JSON response WITH cURL

I don't know what I'm doing wrong but I've already lost a couple days struggling with this.我不知道我做错了什么,但我已经为此苦苦挣扎了几天。

Here is my cURL request from the command line:这是我从命令行发出的 cURL 请求:

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

This returns:这将返回:

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

So everything looks fine: the Content-type is correctly set and response is in JSON.所以一切看起来都很好:Content-type 设置正确,响应是 JSON。

But now watch what happens if I consume the API with curl in PHP:但是现在看看如果我在 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);

I get this response:我得到这个回应:

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

And, if I return the $result without json_decode , I get this:而且,如果我返回没有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"
  }
}"

The correct response but inside quotes.正确的响应,但在引号内。 I've read in PHP docs that curl_opt_returntranfer returns the result as a string but I can't be the only person on the planet that just wants to get the JSON.我在 PHP 文档中curl_opt_returntranfer将结果作为字符串返回,但我不能成为这个星球上唯一想要获取 JSON 的人。

This is my ApiController class:这是我的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(), ] ]); } }

This is my UserController :这是我的UserController

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

And if I return the $result without json_decode i get this: The correct response but inside quotes如果我在没有 json_decode 的情况下返回 $result,我会得到:正确的响应,但在引号内

nope, what makes you think that?不,是什么让你这么想? i guess the problem is how you are printing it, you are most likely printing it with var_export($result) or var_dump($result) or echo json_encode($result);我想问题是你如何打印它,你很可能用var_export($result)var_dump($result)echo json_encode($result); which is adding the quotes.这是添加引号。 if you just want the json, just echo it with echo $result;如果你只想要 json,只需用echo $result; , no extra processing, just echo the string as-is, it's already json. ,没有额外的处理,直接回显字符串,已经是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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM