楽天市場apiでジャンル指定

色々できそうですね♪

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html>
<html lang='ja'>
<head>
<title>api</title>
<meta charset='utf-8'>
</head>
<body>
<?php
$rakuten_result = getRakutenResult('adidas', 1000);
foreach ($rakuten_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><?php echo $item&#91;'shop'&#93;; ?></div>
</div>
</div>
<?php
endforeach;
?>
</body>
</html>
 
 
<?php
 
function getRakutenResult($keyword, $min_price){
 
 
 
function urlencode_rfc3986($str){
    return str_replace('%7E','~', rawurlencode($str));
}
 
$params = array();
$params&#91;'applicationId'&#93; = '***';
$params&#91;'keyword'&#93; = urlencode_rfc3986($keyword);
$params&#91;'sort'&#93; = urlencode_rfc3986('+itemPrice');
$params&#91;'minPrice'&#93; = $min_price;
$params&#91;'genreId'&#93; = '509057';
 
 
 
$canonical_string='';
foreach($params as $k => $v){
    $canonical_string .= '&' .$k. '=' .$v;
}
 
$canonical_string = substr($canonical_string, 1);
 
$url = $baseurl . '?' . $canonical_string;
 
 
$rakuten_json=json_decode(@file_get_contents($url, true));
 
print_r('<pre>');
var_dump($rakuten_json);
print_r('</pre>');
 
$items = array();
foreach($rakuten_json->Items as $item) {
    $items[] = array(
                    'name' => (string)$item->Item->itemName,
                    'url' => (string)$item->Item->itemUrl,
                    'img' => isset($item->Item->mediumImageUrls[0]->imageUrl) ? (string)$item->Item->mediumImageUrls[0]->imageUrl : '',
                    'price' => (string)$item->Item->itemPrice,
                    'shop' => (string)$item->Item->shopName,
                    );
}
 
return $items;
}
?>