楽天APIから商品一覧を取得してみる

PHP
楽天商品検索APIを利用した商品検索サンプルプログラム


検索キーワード: アップル 純正 ワイヤレスイヤホン Blu・・・

アクティブノイズキャンセリング  ノイズキャンセリング機能に・・・

商品情報 商品の説明 包み込むようなサウンドを生み出すアクテ・・・

AirPods Pro MWP22J/A 新品 国内正規品 ・・・

商品情報Apple(アップル)製品に関しましてApple(ア・・・

商品情報商品詳細メーカー: Apple Japan発売日: ・・・

<<ご購入前に必ずご確認下さい>> ■新品未使用品ですが輸送・・・

■商品名:Air Pods pro ■商品状態:SSSランク・・・

「アクティブノイズキャンセリング」を搭載し、耳に入ってくる音・・・

【中古品】AirPods Pro MWP22J/A 第1世代・・・

ソースコードのアプリIDとアフィリエイトIDはご自身で楽天ウェブサービスから取得してください。

<?php 
$page = '';
$keyword = '';

// 検索入力キーワードのチェック
if (isset($_POST['searchtextbox'])) {
	$keyword = $_POST['searchtextbox'];

	// 空白入力
	$pattern="^(\s| )+$";
	if(mb_ereg_match($pattern, $keyword)) {

		// デフォルト設定
		$keyword='4549995085938';
	}
}
 
// 検索キーワードのデフォルト設定(初期値)
if ($keyword == '') {
	$keyword = '4549995085938';
}

// ソートのデフォルト設定 - レビュー件数降順
$sort = '-itemPrice';

// 取得ページの初期設定
if ($page == '') {
	$page = 1;
}

 
// 1ページあたりの取得件数(商品数)
$hits_set = 10;
 
// 検索キーワード入力ボックス
$searchTextBox = '
<form action="" method="POST" name="form">

<input type="text" name="searchtextbox" value="'.$keyword.'" placeholder="商品番号で探す" style="width:240px;height:28px;vertical-align: top;">

<input type="submit" name="btn" value="検索" style="font-size:1.1rem;height:40px;vertical-align: top;">
</form>
';
 
// エンコーディング
$url_keyword = htmlspecialchars(urlencode($keyword));
$url_sort = htmlspecialchars(urlencode($sort));
 
// アプリID
$applicationId='00000000000000000000000000000000';

// アフィリエイトID
$affiliateId='00000000.00000000.00000000.00000000';
 
// 商品情報取得
$rakutenUrl = "https://app.rakuten.co.jp/services/api/IchibaItem/Search/20130805?format=xml&keyword=".$url_keyword."&sort=".$url_sort."&page=".$page."&hits=".$hits_set."&applicationId=".$applicationId."&affiliateId=".$affiliateId;
 
// レスポンス取得
$contents = @file_get_contents($rakutenUrl);
 
// XMLオブジェクトに変換
$xml = simplexml_load_string($contents);
 
print <<< aff
<!DOCTYPE html >
<html lang="ja">
<head>
<meta charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<TITLE>楽天商品検索APIを利用した商品検索サンプルプログラム</TITLE>
<meta name="Keywords" content=""> 
<meta name="Description" content="">
</head>
<body>
<div id="container" style="padding:10px 10px 40px;color:#1D56A5;">
aff;

print '<br />';
 
// 検索キーワード入力ボックス
print '<table><tr><td >'.$searchTextBox.'</td></tr></table>';
 
// 商品表示
print '<table border="0"><tr>';
$i = 0;
foreach($xml->Items->Item as $item){
	$affiliateUrl = $item->affiliateUrl;
	$mediumImageUrl = $item->mediumImageUrls->imageUrl;
	$detail = $item->itemCaption;
	$detail = mb_substr($detail, 0, 30,"UTF-8") .'・・・';
     
	// 商品表示
	print '<td style="padding:10px;">
	<div><a href="'.$affiliateUrl.'" target="_blank"><img src="'.$mediumImageUrl.'"></a><br />'. $detail .' </div>
	</td>';

	$i++;
	if ($i%5 == 0) {
		print '</tr><tr>';
	}
}
print '</table>';
print '</div><!--end/container-->';
print '<br />';
print '</body></html>';
?>

投稿フォームでも動作するので、金額やポイント送料などを加味して表示できるようにしてみたいと思います。

楽天ウェブサービスセンター

コメント

タイトルとURLをコピーしました