いつもお世話になっております。
表題の詳細についてですが、
ショッピング系の商品検索apiを2か所からxmlで取得し、サイトに表示しております。
(わかりやすいようにA_apiとB_apiと表示します。)
現在の表示はA_apiをすべて表示したあとにB_apiを表示しております。
また、ソートはそれぞれのapiの中でのみ機能しています。
これを、2つのapiから取得したデータを合わせた状態でソートをかけたいと思っております。
ソートをかけるには配列に入れることができればソートをかけれるというのは調べましたが、
2つのapiから取得したデータをどのようにして配列にいれればよいでしょうか?
取得する際のxmlのパラメータ名などは統一されていません。
何か考え方のヒントを頂けないでしょうか?
よろしくお願いいたします。
ソースを追記
PHP
1<?php 2/** @mainpage 3 * 商品検索フォームを表示 4 */ 5 6/** 7 * @file 8 * @brief 商品検索フォームを表示 9 * 10 * 商品検索フォームを表示し、 11 * フォームから入力された値を条件に、検索APIを利用して、検索した結果をhtmlに埋め込んで表示します。 12 * 検索結果に対して、カテゴリーによる絞り込みと、並び順の変更ができます。 13 * 14 * PHP version 5 15 */ 16 17require_once("../common/common.php");//共通ファイル読み込み(使用する前に、appidを指定してください。) 18 19$hits = array(); 20$query = !empty($_GET["query"]) ? $_GET["query"] : ""; 21$sort = !empty($_GET["sort"]) && array_key_exists($_GET["sort"], $sortOrder) ? $_GET["sort"] : "-score"; 22$category_id = ctype_digit($_GET["category_id"]) && array_key_exists($_GET["category_id"], $categories) ? $_GET["category_id"] : 1; 23 24if ($query != "") { 25 $query4url = rawurlencode($query); 26 $sort4url = rawurlencode($sort); 27 $url = "http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch?appid=$appid&query=$query4url&category_id=$category_id&sort=$sort4url"; 28 $xml = simplexml_load_file($url); 29 if ($xml["totalResultsReturned"] != 0) {//検索件数が0件でない場合,変数$hitsに検索結果を格納します。 30 $hits = $xml->Result->Hit; 31 } 32} 33?> 34<html> 35 <head> 36 <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> 37 <title>ショッピングデモサイト - 商品を検索する</title> 38 <link rel="stylesheet" type="text/css" href="../css/prototype.css"/> 39 </head> 40 <body> 41 <h1><a href="./ItemSearchForm.php">ショッピングデモサイト - 商品を検索する</a></h1> 42 <form action="./ItemSearchForm.php" class="Search"> 43 表示順序: 44 <select name="sort"> 45 <?php foreach ($sortOrder as $key => $value) { ?> 46 <option value="<?php echo h($key); ?>" <?php if($sort == $key) echo "selected=\"selected\""; ?>><?php echo h($value);?></option> 47 <?php } ?> 48 </select> 49 キーワード検索: 50 <select name="category_id"> 51 <?php foreach ($categories as $id => $name) { ?> 52 <option value="<?php echo h($id); ?>" <?php if($category_id == $id) echo "selected=\"selected\""; ?>><?php echo h($name);?></option> 53 <?php } ?> 54 </select> 55 <input type="text" name="query" value="<?php echo h($query); ?>"/> 56 <input type="submit" value="Yahooショッピングで検索"/> 57 </form> 58 <?php foreach ($hits as $hit) { ?> 59 60 <div class="Item"> 61 <h2><a href="<?php echo h($hit->Url); ?>"><?php echo h($hit->Name); ?></a></h2> 62 <p><a href="<?php echo h($hit->Url); ?>"><img src="<?php echo h($hit->Image->Medium); ?>" /></a><?php echo h($hit->Description); ?></p> 63 </div> 64 <?php } ?> 65 66<?php 67 $r_appid = "省略"; 68 $r_sort = "+itemPrice"; 69 $r_sort4url = rawurlencode($r_sort); 70 $r_url = "https://app.rakuten.co.jp/services/api/IchibaItem/Search/20170706?applicationId=$r_appid&format=xml&keyword=$query&sort=$r_sort4url"; 71 72 $r_xml = simplexml_load_file($r_url); 73 $r_items = $r_xml->Items->Item; 74 ?> 75 <?php } ?> 76 <?php } ?> 77 <?php foreach ($r_items as $r_item) { ?> 78 <div class="Item"> 79 <h2><a href="<?php echo h($r_item -> itemUrl); ?>"><?php echo h($r_item -> shopName); ?></a></h2> 80 <p><a href="<?php echo h($r_item -> itemUrl); ?>"><img src="<?php echo h($r_item -> mediumImageUrls -> imageUrl); ?>" /></a><?php echo h($r_item -> itemName); ?><br><?php echo h($r_item -> itemCode); ?><br><?php echo h($r_item->shopCode); ?><br><?php echo h($r_item -> shopName); ?><br><br>通常価格:<?php echo h($r_item -> itemPrice); ?>円</p> 81 </div> 82 83 <?php } ?> 84<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet --> 85<a href="http://developer.yahoo.co.jp/about"> 86<img src="http://i.yimg.jp/images/yjdn/yjdn_attbtn2_105_17.gif" width="105" height="17" title="Webサービス by Yahoo! JAPAN" alt="Webサービス by Yahoo! JAPAN" border="0" style="margin:15px 15px 15px 15px"></a> 87<!-- End Yahoo! JAPAN Web Services Attribution Snippet --> 88 </body> 89</html>
実際のコードとは異なりますが、おおむねこのような形でxmlを取得しております。
取得したそれぞれのapiを再度合わせてソートをかけれるようにしたいのですが、
どのように共通化すればよいでしょうか?
よろしくお願い致します。
追記2 取得しているxmlを記載
A_api
xml
1<Hit index="1"> 2 <Name>パナソニック 空気清浄機脱臭フィルターF-ZDJD30</Name> 3 <Description> 4対応機種:F-PDJ30-A/F-PDK30-A/F-PDJ30-C/F-PDL30-A/F-PDK30-C/F-PDM30-A/F-PDL30-C/F-PDJ30-P/F-PDK30-P/F-PDL30-P/F-PDM30-W/F-PDP30-W/F-PDR30-W/ ※メーカー取り寄せ品 キャンセル不可 ご注文日から1〜3営業日で出荷 (メーカー在庫切れ、生産完了の場合もございます。その際は後日納期をメールにてご連絡致します。) 5</Description> 6 <Headline/> 7 <Url> 8https://store.shopping.yahoo.co.jp/w-yutori/4942302194260.html 9</Url> 10 <ReleaseDate/> 11 <Availability>instock</Availability> 12 <Code>w-yutori_4942302194260</Code> 13 <Condition>new</Condition> 14 <PersonId/> 15 <ProductId/> 16 <Image> 17 <Id/> 18 <Small>https://s.yimg.jp/images/sh/noimage/76x76.gif</Small> 19 <Medium>https://s.yimg.jp/images/sh/noimage/146x146.gif</Medium> 20 </Image> 21 <Review> 22 <Rate>0.00</Rate> 23 <Count>0</Count> 24 <Url> 25https://shopping.yahoo.co.jp/review/item/list?store_id=w-yutori&page_key=4942302194260 26</Url> 27 </Review> 28 <Affiliate> 29 <Rate>10.0</Rate> 30 </Affiliate> 31 <Price currency="JPY">1640</Price> 32 <PremiumPrice/> 33 <PriceLabel taxIncluded="true"> 34 <FixedPrice/> 35 <DefaultPrice>1640</DefaultPrice> 36 <SalePrice/> 37 <PremiumPriceStatus>0</PremiumPriceStatus> 38 <PremiumPrice>1640</PremiumPrice> 39 <PremiumDiscountType/> 40 <PremiumDiscountRate/> 41 <BaseFixedPrice/> 42 <BaseDefaultPrice>1519</BaseDefaultPrice> 43 <BaseSalePrice/> 44 <BasePremiumPrice>1519</BasePremiumPrice> 45 <PeriodStart>2019-06-24T10:00:00+09:00</PeriodStart> 46 <PeriodEnd>2019-06-26T02:00:00+09:00</PeriodEnd> 47 </PriceLabel> 48 <Point> 49 <Amount>16</Amount> 50 <Times>1</Times> 51 <PremiumAmount>16</PremiumAmount> 52 <PremiumTimes>1</PremiumTimes> 53 </Point> 54 <Shipping> 55 <Code>1</Code> 56 <Name>設定無し</Name> 57 </Shipping> 58 <Category> 59 <Current> 60 <Id>26321</Id> 61 <Name>空気清浄機交換フィルター</Name> 62 </Current> 63 </Category> 64 <CategoryIdPath> 65 <Category> 66 <Id>1</Id> 67 </Category> 68 <Category> 69 <Id>2505</Id> 70 </Category> 71 <Category> 72 <Id>4740</Id> 73 </Category> 74 <Category> 75 <Id>26318</Id> 76 </Category> 77 <Category> 78 <Id>26321</Id> 79 </Category> 80 </CategoryIdPath> 81 <Brands> 82 <Name>パナソニック</Name> 83 <Path> 84 <Brand> 85 <Id>1</Id> 86 </Brand> 87 <Brand> 88 <Id>1460</Id> 89 </Brand> 90 </Path> 91 </Brands> 92 <JanCode/> 93 <Model/> 94 <IsbnCode/> 95 <Store> 96 <Id>w-yutori</Id> 97 <Name>Web Shop ゆとり Yahoo!店</Name> 98 <SellerType>B</SellerType> 99 <Url>https://store.shopping.yahoo.co.jp/w-yutori/</Url> 100 <Payment> 101 <Method> 102 <Code>1</Code> 103 <Name>クレジットカード</Name> 104 </Method> 105 <Method> 106 <Code>16</Code> 107 <Name>Yahoo!ウォレットに登録しているクレジットカード</Name> 108 </Method> 109 <Method> 110 <Code>4</Code> 111 <Name>商品代引</Name> 112 </Method> 113 <Method> 114 <Code>2</Code> 115 <Name>銀行振込</Name> 116 </Method> 117 <Method> 118 <Code>8</Code> 119 <Name>郵便振替</Name> 120 </Method> 121 <Method> 122 <Code>32</Code> 123 <Name>モバイルSuica</Name> 124 </Method> 125 <Method> 126 <Code>64</Code> 127 </Hit>
B_api
xml
1<Item> 2 <itemName> 3【今月限定/特別大特価】AUEA08077M東芝 業務用エアコン スーパーパワーエコmini天井カセット4方向 3馬力 シングル標準省エネ 三相200V ワイヤードAUEA08077Mが激安 4</itemName> 5 <catchcopy> 6《AUEA08077M》《送料無料&メーカー1年保証付》《カード決済もOK》《みんなのレビュー2,200件突破》《安心の楽天EXPO受賞店》 7</catchcopy> 8 <itemCode>setsubi:10158165</itemCode> 9 <itemPrice>162145</itemPrice> 10 <itemCaption> 11メーカー希望小売価格はメーカーカタログに基づいて掲載しています※パネル・リモコンを含んだ金額になります※パネルカ... 12</itemCaption> 13 <itemUrl>https://item.rakuten.co.jp/setsubi/auea08077m/</itemUrl> 14 <shopUrl>https://www.rakuten.co.jp/setsubi/</shopUrl> 15 <smallImageUrls> 16 <imageUrl> 17https://thumbnail.image.rakuten.co.jp/@0_mall/setsubi/cabinet/ind_aircon/toshiba/201604/auea08077m.jpg?_ex=64x64 18</imageUrl> 19 <imageUrl> 20https://thumbnail.image.rakuten.co.jp/@0_mall/setsubi/cabinet/ind_aircon/toshiba/201604_2/roa-ap807h.jpg?_ex=64x64 21</imageUrl> 22 <imageUrl> 23https://thumbnail.image.rakuten.co.jp/@0_mall/setsubi/cabinet/ind_aircon/toshiba/201604_2/rbc-ams53.jpg?_ex=64x64 24</imageUrl> 25 </smallImageUrls> 26 <mediumImageUrls> 27 <imageUrl> 28https://thumbnail.image.rakuten.co.jp/@0_mall/setsubi/cabinet/ind_aircon/toshiba/201604/auea08077m.jpg?_ex=128x128 29</imageUrl> 30 <imageUrl> 31https://thumbnail.image.rakuten.co.jp/@0_mall/setsubi/cabinet/ind_aircon/toshiba/201604_2/roa-ap807h.jpg?_ex=128x128 32</imageUrl> 33 <imageUrl> 34https://thumbnail.image.rakuten.co.jp/@0_mall/setsubi/cabinet/ind_aircon/toshiba/201604_2/rbc-ams53.jpg?_ex=128x128 35</imageUrl> 36 </mediumImageUrls> 37 <affiliateUrl/> 38 <shopAffiliateUrl/> 39 <imageFlag>1</imageFlag> 40 <availability>1</availability> 41 <taxFlag>0</taxFlag> 42 <postageFlag>0</postageFlag> 43 <creditCardFlag>1</creditCardFlag> 44 <shopOfTheYearFlag>0</shopOfTheYearFlag> 45 <shipOverseasFlag>0</shipOverseasFlag> 46 <shipOverseasArea/> 47 <asurakuFlag>0</asurakuFlag> 48 <asurakuClosingTime/> 49 <asurakuArea/> 50 <affiliateRate>2.0</affiliateRate> 51 <startTime/> 52 <endTime/> 53 <reviewCount>22</reviewCount> 54 <reviewAverage>4.82</reviewAverage> 55 <pointRate>1</pointRate> 56 <pointRateStartTime/> 57 <pointRateEndTime/> 58 <giftFlag>0</giftFlag> 59 <shopName>業務用エアコンのセツビコム</shopName> 60 <shopCode>setsubi</shopCode> 61 <genreId>208369</genreId> 62 <tagIds> 63 <value>1002881</value> 64 </tagIds> 65</Item>
xmlは上記ソースで作成したURLから所得しているxmlなので、
実際の店舗情報が記載されています。不都合があった場合削除いたします。
また、今回はやってほしいことだけを記載した丸投げの質問ではなく、
ヒントや考え方のアドバイスが欲しいのであって、答えをそのまま作ってほしいわけではありません。
ソースやデータを記載しなかったのは、取得データやソースを公開するにあたり、api元の権利関係を確認していたからです。
質問の仕方が不適切で申し訳ないです。
回答2件
あなたの回答
tips
プレビュー