通过cURL获取JSON响应

时间:2015-03-29 10:48:00

标签: php json laravel curl

令人难以置信,我不知道自己做错了什么,但我已经失去了几天的挣扎。

这是来自de comand line的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"
  }
}

所以一切都很好,Content-type设置正确,响应是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"
  }
}"

正确的回答,但在引号内,我已经读过phpdocs,curl_opt_returntranfer将结果返回为字符串,但我不能成为地球上唯一想要获得json的人。

请帮忙

编辑1

这是我的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);
	}

}




3 个答案:

答案 0 :(得分:0)

我认为这可以解决您的问题:

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

答案 1 :(得分:0)

$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;

答案 2 :(得分:0)

如果我返回没有json_decode的$ result,我会得到:正确的响应,但在引号内

不,您是怎么想的?我想问题是您如何打印它,您最有可能使用var_export($result)var_dump($result)echo json_encode($result);打印它,并加上引号。如果您只想要json,只需用echo $result;回显它,而无需额外处理,只需按原样回显字符串,它已经是json。