PHP根据关键词获取搜狗搜索图片

180it 2021-05-13 PM 995℃ 0条
<?php
        //搜索指定关键词的搜狗图片并显示
        $keyword = "印度一动物园的8头狮子确诊感染新冠病毒";
        $keyword = urlencode($keyword);
        $url = "https://pic.sogou.com/pics?query=".$keyword."&st=255&from=vr&rawQuery=";
        $html = file_get_contents($url);
        preg_match_all('/picUrl":"(.*?)"/', $html, $text);
        
        
        foreach ($text as $key => $value) {
            foreach ($value as $img) {
                
                
                $img=str_replace('picUrl":"',"",$img);
                $img=str_replace('"',"",$img);
                
                echo unicode_decode2($img).PHP_EOL;
                break;
               // print "<img src=" . unicode_decode($img). " />";
            }
        }
        
        
        
        //Unicode编码转字符串方法1
function unicode_decode2($str){
 $json = '{"str":"' . $str . '"}';
 $arr = json_decode($json, true);
 if (empty($arr)) return '';
 return $arr['str'];
}
        ?>
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

PHP根据关键词获取搜狗搜索图片