2013-01-13 58 views
1

我如何解析以下內容以獲得從亞馬遜Product Advertising API檢索到的產品的description,title,產品編號等...?如何解析來自Amazon Product Advertising API的stdclass對象?

我已經使用了以下內容,但並不一致。

$response = $client->responseGroup('Small,Images')->category('Books')->search($search); 

foreach ($response->Items->Request->Item as $message) { 
    echo $message->Description; 

} 

以下輸出:也可在pastebin找到。下面的代碼是輸出。我想從對象中選擇標題,描述和url數據,而不是以數組格式。

任何想法?

 object(stdClass)#6 (2) { 
    ["OperationRequest"]=> 
    object(stdClass)#7 (4) { 
    ["HTTPHeaders"]=> 
    object(stdClass)#8 (1) { 
     ["Header"]=> 
     object(stdClass)#9 (2) { 
     ["Name"]=> 
     string(9) "UserAgent" 
     ["Value"]=> 
     string(26) "PHP-SOAP/5.3.2-1ubuntu4.18" 
     } 
    } 
    ["RequestId"]=> 
    string(36) "cbcaa978-487c-4d83-8986-6a641b7d9c00" 
    ["Arguments"]=> 
    object(stdClass)#10 (1) { 
     ["Argument"]=> 
     object(stdClass)#11 (2) { 
     ["Name"]=> 
     string(7) "Service" 
     ["Value"]=> 
     string(19) "AWSECommerceService" 
     } 
    } 
    ["RequestProcessingTime"]=> 
    float(0.081366) 
    } 
    ["Items"]=> 
    object(stdClass)#12 (5) { 
    ["Request"]=> 
    object(stdClass)#13 (2) { 
     ["IsValid"]=> 
     string(4) "True" 
     ["ItemSearchRequest"]=> 
     object(stdClass)#14 (3) { 
     ["Keywords"]=> 
     string(9) "airplanes" 
     ["ResponseGroup"]=> 
     array(2) { 
      [0]=> 
      string(5) "Small" 
      [1]=> 
      string(6) "Images" 
     } 
     ["SearchIndex"]=> 
     string(5) "Books" 
     } 
    } 
    ["TotalResults"]=> 
    int(21343) 
    ["TotalPages"]=> 
    int(2135) 
    ["MoreSearchResultsUrl"]=> 
    string(237) "http://www.amazon.com/gp/redirect.html?camp=2025&creative=386001&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fsearch%3Fkeywords%3Dairplanes%26url%3Dsearch-alias%253Dstripbooks&linkCode=sp1&tag=xxxxxxxxxxxxxxbet-20&SubscriptionId=AKIAII3UF3HPAPY3SMDQ" 
    ["Item"]=> 
    array(10) { 
     [0]=> 
     object(stdClass)#15 (8) { 
     ["ASIN"]=> 
     string(10) "0789465213" 
     ["DetailPageURL"]=> 
     string(205) "http://rads.stackoverflow.com/amzn/click/0789465213" 
     ["ItemLinks"]=> 
     object(stdClass)#16 (1) { 
      ["ItemLink"]=> 
      array(7) { 
      [0]=> 
      object(stdClass)#17 (2) { 
       ["Description"]=> 
       string(17) "Technical Details" 
       ["URL"]=> 
       string(215) "http://rads.stackoverflow.com/amzn/click/0789465213" 
      } 
      [1]=> 
      object(stdClass)#18 (2) { 
       ["Description"]=> 
       string(20) "Add To Baby Registry" 
       ["URL"]=> 
       string(211) "http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3D0789465213%26SubscriptionId%3DAKIAII3UF3HPAPY3SMDQ%26tag%3Dxxxxxxxxxxxxxxbet-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0789465213" 
      } 
      [2]=> 
      object(stdClass)#19 (2) { 
       ["Description"]=> 
       string(23) "Add To Wedding Registry" 
       ["URL"]=> 
       string(214) "http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3D0789465213%26SubscriptionId%3DAKIAII3UF3HPAPY3SMDQ%26tag%3Dxxxxxxxxxxxxxxbet-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0789465213" 
      } 
      [3]=> 
      object(stdClass)#20 (2) { 
       ["Description"]=> 
       string(15) "Add To Wishlist" 
       ["URL"]=> 
       string(215) "http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3D0789465213%26SubscriptionId%3DAKIAII3UF3HPAPY3SMDQ%26tag%3Dxxxxxxxxxxxxxxbet-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0789465213" 
      } 
      [4]=> 
      object(stdClass)#21 (2) { 
       ["Description"]=> 
       string(13) "Tell A Friend" 
       ["URL"]=> 
       string(180) "http://rads.stackoverflow.com/amzn/click/0789465213" 
      } 
      [5]=> 
      object(stdClass)#22 (2) { 
       ["Description"]=> 
       string(20) "All Customer Reviews" 
       ["URL"]=> 
       string(184) "http://rads.stackoverflow.com/amzn/click/0789465213" 
      } 
      [6]=> 
      object(stdClass)#23 (2) { 
       ["Description"]=> 
       string(10) "All Offers" 
       ["URL"]=> 
       string(186) "http://rads.stackoverflow.com/amzn/click/0789465213" 
      } 
      } 
     } 
     ["SmallImage"]=> 
     object(stdClass)#24 (3) { 
      ["URL"]=> 
      string(60) "http://ecx.images-amazon.com/images/I/51G392LNnWL._SL75_.jpg" 
      ["Height"]=> 
      object(stdClass)#25 (2) { 
      ["_"]=> 
      string(2) "75" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
      ["Width"]=> 
      object(stdClass)#26 (2) { 
      ["_"]=> 
      string(2) "56" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
     } 
     ["MediumImage"]=> 
     object(stdClass)#27 (3) { 
      ["URL"]=> 
      string(61) "http://ecx.images-amazon.com/images/I/51G392LNnWL._SL160_.jpg" 
      ["Height"]=> 
      object(stdClass)#28 (2) { 
      ["_"]=> 
      string(3) "160" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
      ["Width"]=> 
      object(stdClass)#29 (2) { 
      ["_"]=> 
      string(3) "120" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
     } 
     ["LargeImage"]=> 
     object(stdClass)#30 (3) { 
      ["URL"]=> 
      string(53) "http://ecx.images-amazon.com/images/I/51G392LNnWL.jpg" 
      ["Height"]=> 
      object(stdClass)#31 (2) { 
      ["_"]=> 
      string(3) "500" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
      ["Width"]=> 
      object(stdClass)#32 (2) { 
      ["_"]=> 
      string(3) "376" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
     } 
     ["ImageSets"]=> 
     object(stdClass)#33 (1) { 
      ["ImageSet"]=> 
      object(stdClass)#34 (7) { 
      ["SwatchImage"]=> 
      object(stdClass)#35 (3) { 
       ["URL"]=> 
       string(60) "http://ecx.images-amazon.com/images/I/51G392LNnWL._SL30_.jpg" 
       ["Height"]=> 
       object(stdClass)#36 (2) { 
       ["_"]=> 
       string(2) "30" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#37 (2) { 
       ["_"]=> 
       string(2) "23" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["SmallImage"]=> 
      object(stdClass)#38 (3) { 
       ["URL"]=> 
       string(60) "http://ecx.images-amazon.com/images/I/51G392LNnWL._SL75_.jpg" 
       ["Height"]=> 
       object(stdClass)#39 (2) { 
       ["_"]=> 
       string(2) "75" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#40 (2) { 
       ["_"]=> 
       string(2) "56" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["ThumbnailImage"]=> 
      object(stdClass)#41 (3) { 
       ["URL"]=> 
       string(60) "http://ecx.images-amazon.com/images/I/51G392LNnWL._SL75_.jpg" 
       ["Height"]=> 
       object(stdClass)#42 (2) { 
       ["_"]=> 
       string(2) "75" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#43 (2) { 
       ["_"]=> 
       string(2) "56" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["TinyImage"]=> 
      object(stdClass)#44 (3) { 
       ["URL"]=> 
       string(61) "http://ecx.images-amazon.com/images/I/51G392LNnWL._SL110_.jpg" 
       ["Height"]=> 
       object(stdClass)#45 (2) { 
       ["_"]=> 
       string(3) "110" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#46 (2) { 
       ["_"]=> 
       string(2) "83" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["MediumImage"]=> 
      object(stdClass)#47 (3) { 
       ["URL"]=> 
       string(61) "http://ecx.images-amazon.com/images/I/51G392LNnWL._SL160_.jpg" 
       ["Height"]=> 
       object(stdClass)#48 (2) { 
       ["_"]=> 
       string(3) "160" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#49 (2) { 
       ["_"]=> 
       string(3) "120" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["LargeImage"]=> 
      object(stdClass)#50 (3) { 
       ["URL"]=> 
       string(53) "http://ecx.images-amazon.com/images/I/51G392LNnWL.jpg" 
       ["Height"]=> 
       object(stdClass)#51 (2) { 
       ["_"]=> 
       string(3) "500" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#52 (2) { 
       ["_"]=> 
       string(3) "376" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["Category"]=> 
      string(7) "primary" 
      } 
     } 
     ["ItemAttributes"]=> 
     object(stdClass)#53 (4) { 
      ["Author"]=> 
      string(13) "DK Publishing" 
      ["Manufacturer"]=> 
      string(11) "DK CHILDREN" 
      ["ProductGroup"]=> 
      string(4) "Book" 
      ["Title"]=> 
      string(21) "Big Book of Airplanes" 
     } 
     } 
     [1]=> 
     object(stdClass)#54 (8) { 
     ["ASIN"]=> 
     string(10) "0753459159" 
     ["DetailPageURL"]=> 
     string(211) "http://rads.stackoverflow.com/amzn/click/0753459159" 
     ["ItemLinks"]=> 
     object(stdClass)#55 (1) { 
      ["ItemLink"]=> 
      array(7) { 
      [0]=> 
      object(stdClass)#56 (2) { 
       ["Description"]=> 
       string(17) "Technical Details" 
       ["URL"]=> 
       string(221) "http://rads.stackoverflow.com/amzn/click/0753459159" 
      } 
      [1]=> 
      object(stdClass)#57 (2) { 
       ["Description"]=> 
       string(20) "Add To Baby Registry" 
       ["URL"]=> 
       string(211) "http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3D0753459159%26SubscriptionId%3DAKIAII3UF3HPAPY3SMDQ%26tag%3Dxxxxxxxxxxxxxxbet-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0753459159" 
      } 
      [2]=> 
      object(stdClass)#58 (2) { 
       ["Description"]=> 
       string(23) "Add To Wedding Registry" 
       ["URL"]=> 
       string(214) "http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3D0753459159%26SubscriptionId%3DAKIAII3UF3HPAPY3SMDQ%26tag%3Dxxxxxxxxxxxxxxbet-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0753459159" 
      } 
      [3]=> 
      object(stdClass)#59 (2) { 
       ["Description"]=> 
       string(15) "Add To Wishlist" 
       ["URL"]=> 
       string(215) "http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3D0753459159%26SubscriptionId%3DAKIAII3UF3HPAPY3SMDQ%26tag%3Dxxxxxxxxxxxxxxbet-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0753459159" 
      } 
      [4]=> 
      object(stdClass)#60 (2) { 
       ["Description"]=> 
       string(13) "Tell A Friend" 
       ["URL"]=> 
       string(180) "http://rads.stackoverflow.com/amzn/click/0753459159" 
      } 
      [5]=> 
      object(stdClass)#61 (2) { 
       ["Description"]=> 
       string(20) "All Customer Reviews" 
       ["URL"]=> 
       string(184) "http://rads.stackoverflow.com/amzn/click/0753459159" 
      } 
      [6]=> 
      object(stdClass)#62 (2) { 
       ["Description"]=> 
       string(10) "All Offers" 
       ["URL"]=> 
       string(186) "http://rads.stackoverflow.com/amzn/click/0753459159" 
      } 
      } 
     } 
     ["SmallImage"]=> 
     object(stdClass)#63 (3) { 
      ["URL"]=> 
      string(60) "http://ecx.images-amazon.com/images/I/51VcEPd9jqL._SL75_.jpg" 
      ["Height"]=> 
      object(stdClass)#64 (2) { 
      ["_"]=> 
      string(2) "75" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
      ["Width"]=> 
      object(stdClass)#65 (2) { 
      ["_"]=> 
      string(2) "74" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
     } 
     ["MediumImage"]=> 
     object(stdClass)#66 (3) { 
      ["URL"]=> 
      string(61) "http://ecx.images-amazon.com/images/I/51VcEPd9jqL._SL160_.jpg" 
      ["Height"]=> 
      object(stdClass)#67 (2) { 
      ["_"]=> 
      string(3) "160" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
      ["Width"]=> 
      object(stdClass)#68 (2) { 
      ["_"]=> 
      string(3) "157" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
     } 
     ["LargeImage"]=> 
     object(stdClass)#69 (3) { 
      ["URL"]=> 
      string(53) "http://ecx.images-amazon.com/images/I/51VcEPd9jqL.jpg" 
      ["Height"]=> 
      object(stdClass)#70 (2) { 
      ["_"]=> 
      string(3) "500" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
      ["Width"]=> 
      object(stdClass)#71 (2) { 
      ["_"]=> 
      string(3) "491" 
      ["Units"]=> 
      string(6) "pixels" 
      } 
     } 
     ["ImageSets"]=> 
     object(stdClass)#72 (1) { 
      ["ImageSet"]=> 
      object(stdClass)#73 (7) { 
      ["SwatchImage"]=> 
      object(stdClass)#74 (3) { 
       ["URL"]=> 
       string(60) "http://ecx.images-amazon.com/images/I/51VcEPd9jqL._SL30_.jpg" 
       ["Height"]=> 
       object(stdClass)#75 (2) { 
       ["_"]=> 
       string(2) "30" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#76 (2) { 
       ["_"]=> 
       string(2) "29" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["SmallImage"]=> 
      object(stdClass)#77 (3) { 
       ["URL"]=> 
       string(60) "http://ecx.images-amazon.com/images/I/51VcEPd9jqL._SL75_.jpg" 
       ["Height"]=> 
       object(stdClass)#78 (2) { 
       ["_"]=> 
       string(2) "75" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#79 (2) { 
       ["_"]=> 
       string(2) "74" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["ThumbnailImage"]=> 
      object(stdClass)#80 (3) { 
       ["URL"]=> 
       string(60) "http://ecx.images-amazon.com/images/I/51VcEPd9jqL._SL75_.jpg" 
       ["Height"]=> 
       object(stdClass)#81 (2) { 
       ["_"]=> 
       string(2) "75" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#82 (2) { 
       ["_"]=> 
       string(2) "74" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["TinyImage"]=> 
      object(stdClass)#83 (3) { 
       ["URL"]=> 
       string(61) "http://ecx.images-amazon.com/images/I/51VcEPd9jqL._SL110_.jpg" 
       ["Height"]=> 
       object(stdClass)#84 (2) { 
       ["_"]=> 
       string(3) "110" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#85 (2) { 
       ["_"]=> 
       string(3) "108" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["MediumImage"]=> 
      object(stdClass)#86 (3) { 
       ["URL"]=> 
       string(61) "http://ecx.images-amazon.com/images/I/51VcEPd9jqL._SL160_.jpg" 
       ["Height"]=> 
       object(stdClass)#87 (2) { 
       ["_"]=> 
       string(3) "160" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#88 (2) { 
       ["_"]=> 
       string(3) "157" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["LargeImage"]=> 
      object(stdClass)#89 (3) { 
       ["URL"]=> 
       string(53) "http://ecx.images-amazon.com/images/I/51VcEPd9jqL.jpg" 
       ["Height"]=> 
       object(stdClass)#90 (2) { 
       ["_"]=> 
       string(3) "500" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
       ["Width"]=> 
       object(stdClass)#91 (2) { 
       ["_"]=> 
       string(3) "491" 
       ["Units"]=> 
       string(6) "pixels" 
       } 
      } 
      ["Category"]=> 
      string(7) "primary" 
      } 
     } 
     ["ItemAttributes"]=> 
     object(stdClass)#92 (4) { 
      ["Author"]=> 
      array(2) { 
      [0]=> 
      string(11) "Tony Mitton" 
      [1]=> 
      string(10) "Ant Parker" 
      } 
      ["Manufacturer"]=> 
      string(10) "Kingfisher" 
      ["ProductGroup"]=> 
      string(4) "Book" 
      ["Title"]=> 
      string(36) "Amazing Airplanes (Amazing Machines)" 
     } 
     } 
     [2]=> 
     object(stdClass)#93 (8) { 
     ["ASIN"]=> 
     string(10) "0061143812" 
     ["DetailPageURL"]=> 
     string(214) "http://rads.stackoverflow.com/amzn/click/0061143812" 
     ["ItemLinks"]=> 
     object(stdClass)#94 (1) { 
      ["ItemLink"]=> 
      array(7) { 
      [0]=> 
      object(stdClass)#95 (2) { 
       ["Description"]=> 
       string(17) "Technical Details" 
       ["URL"]=> 
       string(224) "http://rads.stackoverflow.com/amzn/click/0061143812" 
      } 
      [1]=> 
      object(stdClass)#96 (2) { 
       ["Description"]=> 
       string(20) "Add To Baby Registry" 
       ["URL"]=> 
       string(211) "http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3D0061143812%26SubscriptionId%3DAKIAII3UF3HPAPY3SMDQ%26tag%3Dxxxxxxxxxxxxxxbet-20%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0061143812" 


etc.... 

回答

1

首先你可以打印響應陣列來查看陣列格式然後得到你想要元素,例如下面:)

echo '<pre>'; 
print_r($response); 
echo '</pre>'; 

foreach($response['Items']['Item'] as $item) 
{ 

    echo 'asin :'.$item[ASIN].'<br>'; 
    echo 'Title :'.$item[ItemAttributes][Title].'<br>'; 
    echo '<image src="'.$item[MediumImage][URL].'">'; 


} 
+0

您將收到一個錯誤。不能使用stdClass類型的對象作爲數組 – obreezy