<?php
//获取网页
function get_ssl_html($url) {
//file_get_contents()
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function get_thing_images_url_baidu($th) {//获取食材图片url
$thing = urlencode($th);
$url = "https://image.baidu.com/search/index?tn=baiduimage&word=$thing";
$html = get_ssl_html($url);
$pattern = '/thumbURL":"(.+?)"/is';
preg_match_all($pattern, $html, $match);
$arr=array();
foreach ($match[1] as $img){
if (strstr($img,"jpg")) {
$arr[]=$img;
}
}
return $arr;
}
var_dump( get_thing_images_url_baidu("瑞士卷"));
网友评论