php - Accessing Laravel Rest API with curl - JSON -


i'm trying client work api i'm having trouble, i'm new curl maybe can me.

my api controller (laravel.project/api/v1/users/{id}

public function show($user)     {         if ($user == null)             return $this->setstatuscode(404)->respondnotfound('user not found');          return $this->respond($user);     } 

this answer making simple request postman

{      "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"      }  }      response headers:    cache-control → no-cache  connection → keep-alive  content-type → application/json  date → sat, 28 mar 2015 15:44:45 gmt  server → nginx/1.6.2  transfer-encoding → chunked

everything fine till now, then, when use curl in client way:

public function delete($url, $token)      {            $final_url = $this->base_url . $url;            $ch = curl_init();          curl_setopt($ch, curlopt_url, $final_url);          curl_setopt($ch, curlopt_returntransfer, true);          curl_setopt($ch, curlopt_customrequest, 'delete');          curl_setopt($ch, curlopt_httpheader, array('authorization: bearer ' . $token, 'content-type: application/json'));            $result = curl_exec($ch);          curl_close($ch);            return $result;      }

the response not json:

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

it looks json response notice quotes @ begging , @ end, can't parse neither json_encode or json_decode.

i've been struggling while opinion apreciated.

just make sure understand problem, code working well, when making request curl doesn't use content-type header

edit 1:

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

instead of return $result, try:

return response()->json(json_decode($result)); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -