使用REST GET进行JSON响应 - PHP

I am using cURL and REST to access a database and a query search, i am getting the following response when i use echo $response:

HTTP/1.1 200 OK Date: Fri, 05 Aug 2016 06:53:02 GMT Server: Apache Content-Language: en-US RNT-Time: D=58292 t=1470379982660626 RNT-Machine: 128.65 Transfer-Encoding: chunked Content-Type: application/json { "items": [ { "tableName": "Country", "count": 1, "columnNames": [ "id" ], "rows": [ [ "12" ] ] } ], "links": [ { "rel": "self", "href": "https://test.cust.com/services/rest/connect/v1.3/queryResults?query=select%20ID%20from%20CO.Country%20where%20CO.Country.Country=%27USA%27" }, { "rel": "canonical", "href": "https://test.cust.com/services/rest/connect/v1.3/queryResults" }, { "rel": "describedby", "href": "https://test.cust.com/services/rest/connect/v1.3/metadata-catalog/queryResults", "mediaType": "application/schema+json" } ] } 

i tried to use JSON_decode() but so far i got nowhere, how can i get my parameters here ? to be more specific, the "id" value.

If I get you right you have 2 options:

  • turn off receiving headers curl_setopt($ch, CURLOPT_HEADER, 0)
  • break your response by , you'll get header and body
list($header, $body) = explode("

", $response, 2)
$response;//Suppose your respond is this.

$obj = json_decode($response);

Then you can access your parameters. Example: $obj-> items

You don't need to use JSON_decode().

Use:

$obj = $_POST['items']; // for post

for get:

$obj = $_GET['items']; 

The response which posted here is seems like server response sending to the client browser,

so, you have few of options to parse this data

  1. In case of this response must be parsed using PHP then you can try parsing data using parse_str() in PHP by following way.

    if (FALSE !== (stripos($response, '{'))) {
    
    $data = trim(substr($response, stripos($response, '{')));
    
        $data_arr = array();
    
        parse_str($data, $data_arr);
    
        print_R($data_arr);
    
        //Digging down using parse_str() in php
    
    } else {
    
        echo "No data found.";
    }
    
  2. Either use client side javascript/jquery/client side script to parse json from response on browser.

  3. Returning header as array using Curl

With the help of @evilive, i was able to get it to work:

1- turn of receiving headers curl_setopt($ch, CURLOPT_HEADER, 0)

2- $test=$res->items[0]->rows[0]; echo $test[0];