pixtabay apiで遊んでみる その2 画像の取得

apiで取得したデータをfile_get_contents, file_put_contentsで取得する。

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
<?php
 
$apikey = "hogehoge";
$query = "apple+watch";
$type = "photo";
//  "all", "photo", "illustration", "vector"
$cate = "fashion";
// fashion, nature, backgrounds, science, education, people, feelings, religion, health, places, animals, industry, food, computer, sports, transportation, travel, buildings, business, music
$orientation = "";
//"all", "horizontal", "vertical"
$per_page = "";
//Accepted values: 3 - 200
$order = "";
//"popular", "latest"
$per_page = "";
//Accepted values: 3 - 200
 
$baseurl = "https://pixabay.com/api/?key=". $apikey."&q="."&image_type=".$type."&category=".$cate."";
 
$json = file_get_contents($baseurl);
$obj = json_decode($json);
 
 
// print_r('<pre>');
// var_dump($obj);
// print_r('</pre>');
 
foreach($obj->hits as $value){
    $url[] = $value->webformatURL;
    $tags[] = $value->tags;
}
 
 
$date = date("Ymt");
$i=0;
foreach($url as $value){
    $data = file_get_contents($value);
    $pass = "img/" .$date. "-" . $i . ".jpeg";
    file_put_contents($pass, $data);
    $i++;
}
 
echo "<img src='img/".$date."-1.jpeg' >";
?>

結果、取れるには取れるが、file_get_contentsに1毎3秒かかっている。。。20毎で約1分。

しかし、クエリは”apple+watch”なんだけど、えらい写真が来たな。。