WEB API PROGRAMMING!

web creators 2009年7月号
WEB API PROGRAMMING!:サンプルデータ

<注意書き>
※Web APIの仕様が変更されたなどの理由により、記事と動作が異なる場合があります。あらかじめご了承ください。

●●●●●サンプルデータ1のタイトル

coneco.net API(1)

●●●●●サンプルデータ1

<?php
define('API_KEY', '取得したAPIキーを指定してください');

/**
* coneco.net APIに接続する
*
* @param string メソッド名
* @param array その他のリクエストオプション
*/
function request_coneco($uri, $options = array())
{
$url = 'http://api.coneco.net/cws/v1/' . $uri;
$options['apikey'] = API_KEY;
if ($options) {
foreach ($options as $key => $val) {
if ($val) {
$str_params []= sprintf('%s=%s', $key, urlencode($val));
}
}
$url = sprintf('%s?%s', $url, implode('&', $str_params));
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,
CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($ch);
curl_close($ch);
$result = simplexml_load_string($response);
if (! $result) {
return false;
}

return $result;
}

// 検索用のカテゴリを取得する
$categories = request_coneco('SearchCategories');

// 商品を検索する
$item_result = array();
if (isset($_GET['search']) && $_GET['search']) {
$options = array();
$options['keyword'] = $_GET['keyword'];
$options['freeword'] = $_GET['freeword'];
$options['orFlag'] = ($_GET['orFlag']) ? 1 : 0;
$options['maker'] = $_GET['maker'];
$options['brand'] = $_GET['brand'];
$options['available'] = $_GET['available'];
$options['imageFlag'] = ($_GET['imageFlag']) ? 1 : 0;
$options['coneClick'] = ($_GET['coneClick']) ? 1 : 0;
$options['highestPrice'] = $_GET['highestPrice'];
$options['lowestPrice'] = $_GET['lowestPrice'];
$options['sort'] = $_GET['sort'];
$item_result = request_coneco('SearchProducts', $options);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>coneco.net APIを使ってさまざまな条件から商品を検索してみよう</title>
<style text="text/css">
table { border: 1px solid gray; border-collapse: collapse; margin: 15px; width: 200px; height: 400px; float: left; }
table th, table td { border: 1px solid gray; text-align: center; }
div.center { text-align: center; }
.result { width: 450px; }
</style>
</head>

<body style="text-align: center;">
<h2>coneco.net APIを使ってさまざまな条件から商品を検索してみよう</h2>

<p>条件を選択して、「検索」ボタンを押してください。カテゴリ、商品名、フリーワード、のいずれかは入力してください。</p>
<form action="coneco.php" class="search">
<?php if ($categories): ?>
カテゴリ:<select name="categoryId">
<?php foreach ($categories->Category as $cat): ?>
<option value="<?php echo $cat->Id ?>"><?php echo $cat->Name ?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
商品名:<input type="text" name="keyword" value="<?php echo @$_GET['keyword'] ?>">
フリーワード:<input type="text" name="freeword" value="<?php echo @$_GET['freeword'] ?>">
<br>
or検索フラグ:<input type="checkbox" name="orFlag" value="<?php echo @$_GET['orFlag'] ?>">
メーカー名:<input type="input" name="maker" value="<?php echo @$_GET['maker'] ?>">
ブランド名:<input type="input" name="brand" value="<?php echo @$_GET['brand'] ?>">
<br>
取扱いショップの有無:<input type="checkbox" name="available">
商品画像の有無:<input type="checkbox" name="imageFlag">
こねクリボーナス対象フラグ:<input type="checkbox" name="coneClick">
<br>
最大価格:<input type="text" name="highestPrice" value="<?php echo @$_GET['highestPrice'] ?>">
最小価格:<input type="text" name="lowestPrice" value="<?php echo @$_GET['lowestPrice'] ?>">
並び順:
<select name="sort">
<option value="comId">新しく登録された順</option>
<option value="price">安値順</option>
<option value="-price">高値順</option>
<option value="ranking">ランキング順</option>
<option value="coneClick">こねクリクリックボーナス順</option>
</select>
<br>
<input type="submit" name="search" value="検索する">
</form>

<?php if ($item_result): ?>
<?php foreach ($item_result->Item as $item): ?>
<table>
<tr>
<td colspan="2"><a href="<?php echo $item->Url ?>"><?php echo $item->Name ?></a></td>
</tr>
<tr>
<td colspan="2"><img src="<?php echo $item->ImageUrl ?>"></td>
</tr>
<tr>
<td>最安値</td><td><?php echo number_format($item->LowestPrice) ?>円</td>
</tr>
<tr>
<td>最高値</td><td><?php echo number_format($item->HightestPrice) ?>円</td>
</tr>
<tr>
<td>平均価格</td><td><?php echo number_format($item->AveragePrice) ?>円</td>
</tr>
<tr>
<td>取扱いショップ数</td><td><?php echo $item->SellerCount ?></td>
</tr>
<tr>
<td>順位</td><td><?php echo number_format($item->Ranking) ?></td>
</tr>
<tr>
<td>メーカー</td><td><?php echo $item->Manufacturer ?></td>
</tr>
<tr>
<td>ブランド</td><td><?php echo $item->Brand ?></td>
</tr>
<tr>
<td>発売日</td><td><?php echo $item->ReleaseDate ?></td>
</tr>
<tr>
<td>商品説明</td><td><?php echo $item->Description ?></td>
</tr>
</table>
<?php endforeach; ?>
<?php endif; ?>

<br clear="both">
<hr>
<a href="http://www.coneco.net/" target="_blank" title="powered by coneco.net"><img border="0" src="http://img.coneco.net/images/powered_by_coneco.gif" width="100" height="30" alt="powered by coneco.net"></a>
</body>
</html>

●●●●●サンプルデータ2のタイトル

coneco.net API(2)

●●●●●サンプルデータ2

<?php
define('API_KEY', '取得したAPIキーを指定してください');

/**
* coneco.net APIに接続する
*
* @param string メソッド名
* @param array その他のリクエストオプション
*/
function request_coneco($uri, $options = array())
{
$url = 'http://api.coneco.net/cws/v1/' . $uri;
$options['apikey'] = API_KEY;
if ($options) {
foreach ($options as $key => $val) {
if ($val) {
$str_params []= sprintf('%s=%s', $key, urlencode($val));
}
}
$url = sprintf('%s?%s', $url, implode('&', $str_params));
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,
CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($ch);
curl_close($ch);
$result = simplexml_load_string($response);
if (! $result) {
return false;
}

return $result;
}

// 検索用のカテゴリを取得する
$categories = request_coneco('SearchCategories');

// レビューを検索する
$review_result = array();
if (isset($_GET['search']) && $_GET['search']) {
$options = array();
$options['keyword'] = $_GET['keyword'];
$review_result = request_coneco('SearchReviews', $options);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>coneco.net APIを使ってさまざまな条件からレビューを検索してみよう</title>
<style text="text/css">
table { border: 1px solid gray; border-collapse: collapse; margin: 15px; width: 200px; height: 400px; float: left; }
table th, table td { border: 1px solid gray; text-align: center; }
div.center { text-align: center; }
.result { width: 450px; }
</style>
</head>

<body style="text-align: center;">
<h2>coneco.net APIを使ってさまざまな条件からレビューを検索してみよう</h2>

<p>条件を選択して、「検索」ボタンを押してください。</p>
<form action="coneco2.php" class="search">
<?php if ($categories): ?>
カテゴリ:<select name="categoryId">
<?php foreach ($categories->Category as $cat): ?>
<option value="<?php echo $cat->Id ?>"><?php echo $cat->Name ?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
商品名:<input type="text" name="keyword" value="<?php echo @$_GET['keyword'] ?>">
並び順:
<select name="sort">
<option value="comId">新しく登録された順</option>
<option value="price">安値順</option>
<option value="-price">高値順</option>
<option value="ranking">ランキング順</option>
<option value="coneClick">こねクリクリックボーナス順</option>
</select>
<br>
<input type="submit" name="search" value="検索する">
</form>

<?php if ($review_result): ?>
<?php foreach ($review_result->ItemInfo as $review): ?>
<table>
<?php $item = $review->Item ?>
<tr>
<td colspan="2"><a href="<?php echo $item->Url ?>"><?php echo $item->Name ?></a></td>
</tr>
<tr>
<td colspan="2"><img src="<?php echo $item->ImageUrl ?>"></td>
</tr>
<tr>
<td>平均価格</td><td><?php echo number_format($item->AveragePrice) ?>円</td>
</tr>
<tr>
<td>メーカー</td><td><?php echo $item->Manufacturer ?></td>
</tr>
<tr>
<td colspan="2">レビュー</td>
</tr>
<?php foreach ($review->Reviews as $r): ?>
<?php $review_item = $r->Review ?>
<tr>
<td>タイトル</td><td><a href="<?php echo $review_item->Url ?>"><?php echo $review_item->Summary ?></a></td>
</tr>
<tr>
<td>ユーザマイページ</td><td><a href="<?php echo $review_item->AuthorUrl ?>"><?php echo $review_item->UserId ?></a></td>
</tr>
<tr>
<td>よい点</td><td><?php echo $review_item->Pros ?></td>
</tr>
<tr>
<td>悪い点</td><td><?php echo $review_item->Cons ?></td>
</tr>
<tr>
<td>満足度</td><td><?php echo $review_item->OverallRating ?></td>
</tr>
<tr>
<td>レビュー内容</td><td><?php echo $review_item->Description ?></td>
</tr>
<tr>
<td colspan="2">購入情報</td>
</tr>
<tr>
<td>購入金額</td><td><?php echo number_format($review_item->Purchase->Price) ?>円</td>
</tr>
<tr>
<td>購入日時</td><td><?php echo $review_item->Purchase->Date ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endforeach; ?>
<?php endif; ?>

<br clear="both">
<hr>
<a href="http://www.coneco.net/" target="_blank" title="powered by coneco.net"><img border="0" src="http://img.coneco.net/images/powered_by_coneco.gif" width="100" height="30" alt="powered by coneco.net"></a>
</body>
</html>


●●●●●

←BACK

Copyright (c) 2009 MdN Corporation  All rights reserved.