Question

I'm creating a Restful web service using Django Rest Framework,

I use PHP to do the request:

$url = 'http://localhost:8000/cities/';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

here's what I got:

string(88) "[{"id": 1, "code": "BJG", "name": "Beijing"}, {"id": 2, "code": "TKY", "name": "Tokyo"}]"

What's the most common way/best practice to process the string ? thanks :)

Was it helpful?

Solution

That would be a JSON response.

Best way to process that response would be using json_decode() function, and you can use the json_encode function to do the reverse

EDIT

Quick summary:

json_decode() converts JSON String into a PHP array.
json_encode() converts PHP array into a usable JSON string.

Also: Here's the PHP manual; http://www.php.net/manual/en/function.json-decode.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top