2013-08-23 85 views
0

我想解碼我從ebay查找API收到的JSON響應。我怎麼能得到它以迴應ack消息?Php json_decode()與易趣API

這裏是JSON響應:

{ 
    "findItemsByCategoryResponse":[ 
     { 
     "ack":[ 
      "Success" 
     ], 
     "version":[ 
      "1.12.0" 
     ], 
     "timestamp":[ 
      "2013-08-23T10:12:22.085Z" 
     ], 
     "searchResult":[ 
      { 
       "@count":"1", 
       "item":[ 
        { 
        "itemId":[ 
         "121158900630" 
        ], 
        "title":[ 
         "SHAMROCK SHORES, TX - MONTHLY PAYMENTS - 0 INTEREST" 
        ], 
        "globalId":[ 
         "EBAY-US" 
        ], 
        "subtitle":[ 
         "blocks from Lake Brownwood" 
        ], 
        "primaryCategory":[ 
         { 
          "categoryId":[ 
           "15841" 
          ], 
          "categoryName":[ 
           "Land" 
          ] 
         } 
        ], 
        "galleryURL":[ 
         "http:\/\/thumbs3.ebaystatic.com\/m\/mP-wKIofn8JI9YsMduq6eUw\/140.jpg" 
        ], 
        "viewItemURL":[ 
         "http:\/\/www.ebay.com\/itm\/SHAMROCK-SHORES-TX-MONTHLY-PAYMENTS-0-INTEREST-\/121158900630?pt=Land" 
        ], 
        "autoPay":[ 
         "false" 
        ], 
        "postalCode":[ 
         "76801" 
        ], 
        "location":[ 
         "Brownwood,TX,USA" 
        ], 
        "country":[ 
         "US" 
        ], 
        "shippingInfo":[ 
         { 
          "shippingType":[ 
           "FreePickup" 
          ], 
          "shipToLocations":[ 
           "None" 
          ], 
          "expeditedShipping":[ 
           "false" 
          ], 
          "oneDayShippingAvailable":[ 
           "false" 
          ] 
         } 
        ], 
        "sellingStatus":[ 
         { 
          "currentPrice":[ 
           { 
           "@currencyId":"USD", 
           "__value__":"26.0" 
           } 
          ], 
          "convertedCurrentPrice":[ 
           { 
           "@currencyId":"USD", 
           "__value__":"26.0" 
           } 
          ], 
          "bidCount":[ 
           "4" 
          ], 
          "sellingState":[ 
           "Active" 
          ], 
          "timeLeft":[ 
           "P0DT1H12M13S" 
          ] 
         } 
        ], 
        "listingInfo":[ 
         { 
          "bestOfferEnabled":[ 
           "false" 
          ], 
          "buyItNowAvailable":[ 
           "false" 
          ], 
          "startTime":[ 
           "2013-08-13T11:24:35.000Z" 
          ], 
          "endTime":[ 
           "2013-08-23T11:24:35.000Z" 
          ], 
          "listingType":[ 
           "Auction" 
          ], 
          "gift":[ 
           "false" 
          ] 
         } 
        ], 
        "returnsAccepted":[ 
         "false" 
        ], 
        "isMultiVariationListing":[ 
         "false" 
        ], 
        "topRatedListing":[ 
         "false" 
        ] 
        } 
       ] 
      } 
     ], 
     "paginationOutput":[ 
      { 
       "pageNumber":[ 
        "1" 
       ], 
       "entriesPerPage":[ 
        "1" 
       ], 
       "totalPages":[ 
        "631" 
       ], 
       "totalEntries":[ 
        "631" 
       ] 
      } 
     ], 
     "itemSearchURL":[ 
      "http:\/\/www.ebay.com\/sch\/15841\/i.html?_ddo=1&_ipg=1&_pgn=1" 
     ] 
     } 
    ] 
} 

以下是我的PHP的代碼:

$url = "http://svcs.ebay.com/services/search/FindingService/v1?" . 
     "OPERATION-NAME=findItemsByCategory&" . 
     "SERVICE-VERSION=1.0.0&" . 
     "SECURITY-APPNAME=YourKeyHere&" . 
     "RESPONSE-DATA-FORMAT=JSON&" . 
     "REST-PAYLOAD&" . 
     "categoryId=15841&" . 
     "paginationInput.entriesPerPage=1"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch); 
curl_close($ch);  

$json = json_decode($output, true); 

$status = $json['findItemsByCategory']['ack']; 
echo $status; 

回答

2

findItemsByCategory是一個數組,所述第一元件,其具有ack

$status = $json['findItemsByCategory'][0]['ack']; 
// -----------------------------------^^^ 

奇怪的是,ack也是一個數組,所以得到文本"Success"你不得不提領的是,太:

$status = $json['findItemsByCategory'][0]['ack'][0]; 
// -----------------------------------^^^ ----^^^ 
0

假設$ JSON包含receied JSON響應,您可以使用以下:

$obj = json_decode($json); 
$status = $obj->findItemsByCategoryResponse[0]->ack;