dongliang1893
dongliang1893
2016-08-05 06:59

使用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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • duanhuangyun3887 duanhuangyun3887 5年前

    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)
    
    点赞 评论 复制链接分享
  • dongtuji0992 dongtuji0992 5年前

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

    点赞 评论 复制链接分享
  • douchangmian0305 douchangmian0305 5年前

    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
    点赞 评论 复制链接分享
  • douji2283 douji2283 5年前

    You don't need to use JSON_decode().

    Use:

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

    for get:

    $obj = $_GET['items']; 
    
    点赞 评论 复制链接分享
  • douguabu8960 douguabu8960 5年前
    $response;//Suppose your respond is this.
    
    $obj = json_decode($response);
    

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

    点赞 评论 复制链接分享

相关推荐