php pacong_1.php "酒","電報","電錶","電波","電廠","電場","電車","電池","電傳","電磁",
<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
$word2=explode(",",$argv[1]);
$pageNum = 35;
$uuid = md5(uniqid(mt_rand(), true));
//獲取數據
foreach($word2 as $k=>$v) {
$encodeWord = urlencode(yang_gbk2utf8($v));
for ($i=1; $i < $pageNum; $i++) {
$page = ($i)*30;
//百度頭像圖片獲取api
$url = "http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&ie=utf-8&oe=utf-8&word=".$encodeWord."&pn=".$page."&rn=30&gsm=700001e&1457697756442=";
echo $url."\r\n";
$imgJsonData = file_get_contents($url);
//隨機採樣,讓每次獲取的圖片儘可能不是相同的,能夠增長獲取圖片api去減小獲取重複的圖片的概率
$imgJsonData = json_decode($imgJsonData,true);
//print_r($imgJsonData);
foreach ($imgJsonData['data'] as $key => $dataArr) {
if ($dataArr['thumbURL']) {
downloadImg($dataArr['thumbURL'],$uuid.$k);
}
}
}
}php
function downloadImg($url,$k) {
$path = './userSysAvatorUrl/'.$k."/";
if (!is_dir($path)) {
mkdir($path, 0777, true);
}
$headers=array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Connection: keep-alive",
"Host: img0.imgtn.bdimg.com",
"Referer: http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk&word=%CD%B7%CF%F1&fr=ala&oriquery=%E5%A4%B4%E5%83%8F&ala=1&alatpl=portait&pos=0",
"User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0",
"X-Requested-With: XMLHttpRequest"
);
$ch = curl_init();
//設置選項,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//執行並獲取HTML文檔內容
$imgBin = curl_exec($ch);
curl_close($ch);
list($msec, $sec) = explode(' ', microtime());
$file_name = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
$shullf = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$rand_key = array_rand($shullf,3);
$file_name = $shullf[$rand_key[0]].$shullf[$rand_key[1]].$shullf[$rand_key[2]].'_'.$file_name. '.jpg';
file_put_contents($path.$file_name, $imgBin);
//釋放curl句柄
}html
function yang_gbk2utf8($str){
$charset = mb_detect_encoding($str,array('UTF-8','GBK','GB2312'));
$charset = strtolower($charset);
if('cp936' == $charset){
$charset='GBK';
}
if("utf-8" != $charset){
$str = iconv($charset,"UTF-8//IGNORE",$str);
}
return $str;
}web
?>json