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

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

<?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”なんだけど、えらい写真が来たな。。