yahooショッピングapi

久しぶりにValueCommerceにログインしました(笑)

<!DOCTYPE html>
<html lang='ja'>
<head>
<title>API</title>
<meta charset='utf-8'>
</head>
<body>
<?php

$yahoo_result = getYahooResult('ヴィトン');
foreach ($yahoo_result as $item) :
?>
<div style='margin-bottom: 20px; padding: 30px; border: 1px solid #000; overflow:hidden;'>
<div style='float: left;'><img src='<?php echo $item&#91;'img'&#93;; ?>'></div>
<div style='float: left; padding: 20px;'>
<div><?php echo $item&#91;'name'&#93;; ?></div>
<div><a href='<?php echo $item&#91;'url'&#93;; ?>' target="_blank"><?php echo $item&#91;'url'&#93;; ?></a></div>
<div><?php echo $item&#91;'price'&#93;; ?>円</div>
</div>
</div>
<?php
endforeach;
?>
</body>
</html>
<?php
function getYahooResult($keyword){

$baseurl = "http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch";

$params = array();
$params&#91;"appid"&#93; = "***";
$params&#91;"query"&#93; = urlencode_rfc3986($keyword);
$params&#91;"sort"&#93; = urlencode_rfc3986("+price");
$params&#91;"affiliate_type"&#93; = "vc";
$params&#91;"affiliate_id"&#93; = urlencode_rfc3986("***");

$canonical_string = "";
foreach ($params as $k => $v){
	$canonical_string .= "&" . $k . "=" . $v;
}
$canonical_string = substr($canonical_string, 1);

$url = $baseurl . "?" . $canonical_string;

// echo $url . "<br>";

$yahoo_xml = simplexml_load_string(@file_get_contents($url));

// print_r('<pre>');
// var_dump($yahoo_xml);
// print_r('</pre>');

$items = array();
foreach($yahoo_xml->Result->Hit as $item){

	$items[] = array(
		'name' => (string)$item->Name,
		'url' => (string)$item->Url,
		'img' => (string)$item->Image->Medium,
		'price' => (string)$item->Price,
		);
}
var_dump($items);
return $items;
}

function urlencode_rfc3986($str){
	return str_replace("%7E", "~", rawurlencode($str));
}
?>