検索クエリは一緒で、言語を&lang=ja と&lang=enした時のレスポンス
->画像のURLは一緒
->タグはこちら
Mr.Hans Braxmeier、さすがに翻訳は諦めて形態素解析で手を打ったのかな。。
随机应变 ABCD: Always Be Coding and … : хороший
検索クエリは一緒で、言語を&lang=ja と&lang=enした時のレスポンス
->画像のURLは一緒
->タグはこちら
Mr.Hans Braxmeier、さすがに翻訳は諦めて形態素解析で手を打ったのかな。。
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”なんだけど、えらい写真が来たな。。
<?php $apikey = "hoge"; $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>');
“webformatURL”だと、600x420pxですね。
写真画像ならJPEGが一般的、非可逆圧縮の画像フォーマット
256色以下ならGiF ロゴ、ボタン、イラストなど
PNG 可逆圧縮の画像形式、背景透過も可能、ウェブ用として有用
まず、最初の状態。
6毎の画像があります。
<?php //画像一覧んを取得 foreach(glob('img/*') as $file){ if(is_file($file)){ $file_list[] = $file; // $file_list[] = str_replace('img/', '', $file); } } //ファイル名でソート sort($file_list); var_dump($file_list); echo '<br>'; //古いファイルを切り出す $result = array_slice($file_list, 0, 3); var_dump($result); //取得したファイルをunlinkで削除 foreach($result as $value){ unlink($value); } //結果を表示 foreach(glob('img/*') as $file){ if(is_file($file)){ $file_list[] = $file; } } sort($file_list); var_dump($file_list);
結果
なるほど、なんとなく見えてきましたね。
まず初期状態
unlinkを使います。
<?php unlink('sample.txt'); unlink('03.jpeg'); ?>
削除されました。なるほど。
複数のファイル削除は、glob関数
<?php foreach(glob('*.jpeg') as $file){ unlink($file); echo $file.'を削除しました。<br>'; } ?>
特定フォルダのファイル名一覧の取得もglobでok
<?php foreach(glob('img/*') as $file){ if(is_file($file)){ $file_list[] = str_replace('img/', '', $file); } } var_dump($file_list); ?>
array(3) { [0]=> string(17) “20180331-City.jpg” [1]=> string(19) “20180331-Cuisse.jpg” [2]=> string(19) “20180331-Nomura.jpg” }
foreachで回し、パスをユニークにします。
<?php $img = array( "01.jpeg", "02.jpeg", "03.jpeg" ); $i=0; foreach($img as $value){ $pass = "img/" . $i . ".jpeg"; file_put_contents($pass, $value); $i++; } ?>
おお、どうやら行けそうですね。
では、連想配列ではどうでしょうか?
日付を加えてみます。
<?php $date = date("Ymt"); $img = array( 'Nomura'=>'01.jpeg', 'Cuisse'=>'02.jpeg', 'City'=>'03.jpeg' ); foreach($img as $key => $value){ $pass = "img/" .$date. "-". $key . ".jpg"; file_put_contents($pass, $value); } ?>
問題ないです。
テキストを書き込む場合も、画像と同じ要領です。
<?php $short = '2018/02/26 Credit Suisse Securities 0.470% -0.150% 42,700株 -13,300\n'; file_put_contents("sample.txt", $short); ?>
書き込まれました。
追加する場合は、第三引数にFILE_APPENDとします。
<?php $short = "2018/02/27 Nomura International 5.940% +0.250% 535,100株 +22,700\r\n"; file_put_contents("sample.txt", $short, FILE_APPEND); ?>
上手く追加されてますね。
ではテキストを配列で持っていた場合はどうでしょうか? foreachで回してみます。
<?php $short = array( "2018/02/21 Nomura International 5.230% +0.120% 471,100株 +10,800\r\n", "2018/02/22 Nomura International 6.020% +0.790% 542,500株 +71,400\r\n", "2018/02/23 Nomura International 6.160% +0.140% 555,100株 +12,600\r\n" ); foreach($short as $value){ file_put_contents("sample.txt", $value, FILE_APPEND); } ?>
おおお、入ってますね。少し感動しました。
東証の写真
file_get_contentsで取得した画像を、指定のフォルダに名前を付けて画像に置き換えます。
<?php $img = '01.jpeg'; $data = file_get_contents($img); $pass='img/sec.jpg'; file_put_contents($pass,$data); ?>
実行すると、ファイルが出来ています。$imgのパスは外部サーバーでも同じです。
gif-> jpgでもできました。
まず、mysqlにデータを入れます。
mysql> insert into kabuki(name, lat, lng) values('歌舞伎町店', '35.6969674', '139.700469'); Query OK, 1 row affected (0.10 sec) mysql> select * from kabuki; +-----------------+-----------+------------+ | name | lat | lng | +-----------------+-----------+------------+ | 歌舞伎町店 | 35.696967 | 139.700469 | +-----------------+-----------+------------+ 1 row in set (0.05 sec)
pdo exec(‘TRUNCATE TABLE kabuki’)でデータを削除します。
<?php $dsn = "mysql:dbname=map;host=localhost"; $user = "hoge"; $password = "hogehoge"; try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e){ print('connection failed:'.$e->getMessage()); } $dbh->exec('TRUNCATE TABLE kabuki'); ?>
削除されてますね。
mysql> select * from kabuki; Empty set (0.10 sec)