Skip to content

Instantly share code, notes, and snippets.

@MrCheatEugene
Last active July 12, 2022 20:21
Show Gist options
  • Save MrCheatEugene/01dd359b3d358f5763b3878a9d1f9fb6 to your computer and use it in GitHub Desktop.
Save MrCheatEugene/01dd359b3d358f5763b3878a9d1f9fb6 to your computer and use it in GitHub Desktop.
PHP API для поиска изображений хрущёвок с лицензией Creative Commons... | PHP API for searching images of Khrushchev-designed buildings with Creative Commons License..
<?php
function getPics(){
$dom = new DOMDocument();
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36\r\n" // i.e. An iPad
)
);
$context = stream_context_create($options);
$ch = file_get_contents("https://www.google.com/search?q=%D1%85%D1%80%D1%83%D1%89%D1%91%D0%B2%D0%BA%D0%B0&tbm=isch&hl=ru&tbs=il:cl&prmd=inmv&sa=X", false, $context);
$dom->loadHTML($ch);
$archors = $dom->getElementsByTagName('a');
$images = array();
foreach ($archors as $a) {
if(strpos($a->getAttribute("href"),"commons.wikimedia")>-1){
//echo $a->getAttribute("href")."\n";
$images[count($images)] = json_decode(file_get_contents("https://en.wikipedia.org/w/api.php?action=query&titles=File:".strval(explode("File:",$a->getAttribute("href"))[1])."&prop=imageinfo&iilimit=50&iiend=2007-12-31T23:59:59Z&iiprop=timestamp|user|url&format=json"),true)['query']['pages']['-1']['imageinfo'][0]['url'];
}
}
return $images;
}
<?php
// Добавьте это в ваш Crontab
// Add this to your Crontab
// */30 * * * * /usr/bin/php8.0 ximgs.php > dev/null 2>&1
libxml_use_internal_errors(true);
include 'getimgs.php';
file_put_contents("ximgs.json",json_encode(getPics(),JSON_UNESCAPED_UNICODE));
<?php
libxml_use_internal_errors(true);
header("Content-Type: image/png");
//include 'getimgs.php';
function getImg($imgs){
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36\r\n" // i.e. An iPad
)
);
$context = stream_context_create($options);
return file_get_contents($imgs[array_rand($imgs)],false,$context);
}
echo getImg(json_decode(file_get_contents("ximgs.json")));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment