2016-08-05 137 views
0

我使用捲曲和REST訪問數據庫和查詢搜索,我得到如下回應當我使用echo $responseJSON響應 - PHP

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

我試圖用JSON_decode()但如此遠我沒有任何地方,我怎麼能在這裏得到我的參數?更具體地說,「id」值。

+0

你在客戶端還是服務器?即請求是從客戶端到服務器 - 服務器還是服務器? –

+0

@GoldunoSupport客戶端服務器 – Zame

+0

請求是通過ajax?如果使用\t data = JSON.parse(data);數據是來自服務器的響應。 –

回答

1

如果我得到你的權利,你有兩個選擇:

  • 關閉接收頭curl_setopt($ch, CURLOPT_HEADER, 0)
  • 打破你的迴應通過\r\n\r\n,你會得到headerbody
list($header, $body) = explode("\r\n\r\n", $response, 2) 
+0

通過刪除標題幫助我了很多,現在我得到了完整的JSON響應,我添加了:'$ test = $ res-> items [0] - > rows [0]; echo $ test [0];' 我得到了行值,謝謝 – Zame

-2
$response;//Suppose your respond is this. 

$obj = json_decode($response); 

然後你可以訪問你的參數。例如: $ obj-> items

0

您不需要使用JSON_decode()。

用途:

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

對於GET:

$obj = $_GET['items']; 
1

其張貼在這裏似乎是服務器響應發送到客戶端瀏覽器的響應,

所以,你有幾個選項來解析這個數據

  1. 如果此響應必須使用PHP解析,那麼您可以嘗試使用PHP中的parse_str()解析數據,方法如下。

    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. 要麼使用客戶端的JavaScript/jquery的/客戶端腳本從瀏覽器響應解析JSON。

  3. Returning header as array using Curl
+0

好吧,我得到了'Array([{____「items」:_] => Array([{「tableName」:「CO.Country」,「 count「:1,」columnNames「:[」id「] =>從CO.Country中選擇ID,其中CO.Country。Country ='USA'「},{」rel「:」canonical「,」href「:」https://test.cust.com/services/rest/connect/v1.3/queryResults「},{」rel「 :「describeby」,「href」:「https://test.cust.com/services/rest/connect/v1.3/metadata-catalog/queryResults」,「mediaType」:「application/schema json」}]} ))' 我現在怎麼能得到id屬性? – Zame

+0

然後你需要挖掘dipper並迭代響應數據。 –

0

隨着@evilive的幫助下,我能得到它的工作:接收頭curl_setopt的

1圈($ CH,CURLOPT_HEADER,0)

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