<?php
/**
* Curl Multithreading Download class
*/
Class Multihttprequest
{
Public $urls = Array ();
Private $res = Array ();
Private $curlopt _header = 0;
Private $method = "GET";
Private $curlopt = Array ();
Public function __construct ($urls = false, $curlopt = Array ())
{
$this->urls = $urls;
if (!empty ($curlopt)) {
$this->curlopt = $curlopt;
}
}
Public Function Set_urls ($urls)
{
$this->urls = $urls;
return $this;
}
Public Function set_curlopt ($name, $vale)
{
$this->curlopt[$name] = $vale;
}
Public Function Is_return_header ($b)
{
$this->curlopt_header = $b;
return $this;
}
Public Function get_curlopt ()
{
return $this->curlopt;
}
Public Function Set_method ($m)
{
$this->medthod = Strtoupper ($m);
return $this;
}
Public Function set_curlopts ($arr)
{
$this->curlopt = $arr;
}
Public Function Start ()
{
if (!is_array ($this->urls) | | count ($this->urls) = = 0) {
return false;
}
$handle = Curl_multi_init ();
foreach ($this->urls as $k = + $v) {
$curl [$k] = $this->add_handle ($handle, $v);
}
$this->exec_handle ($handle);
foreach ($this->urls as $k = + $v) {
$this->res[$k] = curl_multi_getcontent ($curl [$k]);
Curl_multi_remove_handle ($handle, $curl [$k]);
}
Curl_multi_close ($handle);
}
Private Function Add_handle ($handle, $url)
{
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);
foreach ($this->curlopt as $k = + $v) {
curl_setopt ($curl, $k, $v);
}
Curl_multi_add_handle ($handle, $curl);
return $curl;
}
Private Function Exec_handle ($handle)
{
$flag = null;
do {
Curl_multi_exec ($handle, $flag);
} while ($flag > 0);
}
Public Function Getres ()
{
return $this->res;
}
}
Class Hdbutifulygril
{
private $url = ';
Private $savepath = ';
Private $imagessavepath = Array ();
Public function __construct ($url, $savepath = ")
{
$this->url = $url;
if (!empty ($savepath) &&!is_dir ($savepath)) {
mkdir ($savepath, 0777,true);
}
$this->savepath = $savepath;
}
/**
* Get the URL address of all large graphs
* @param type $id
* @return Array ();
*/
Private Function Getimagesurl ($id = ")
{
$url = $this->url. $id;
$str = file_get_contents ($url);
$arr = Json_decode ($str, true);
$images = Array ();
$savepath = Array ();
if (Is_array ($arr) &&!empty ($arr)) {
foreach ($arr [' data '] as $v) {
$url = $v [' Image '] [' original '];
$filename = $this->getfilenameby ($url);
if (!file_exists ($filename)) {//If the picture has been collected, don't pick it up again.
$images [] = $url;
$savepath [] = $filename;
}
}
}
$this->imagessavepath = $savepath;
return $images;
}
/**
* URL to determine the location of the image to save
* @param string $url
* @return String
*/
Private Function Getfilenameby ($url)
{
$tmp = explode ('/', $url);
Return $this->savepath. Str_replace (', ', ' _ ', Array_pop ($tmp));
}
/**
* Start Download
* @param int $id pid
*/
Public Function Start ($id = ")
{
$imgurl = $this->getimagesurl ($id);
$imgs = $this->multidownbyurls ($imgurl);
foreach ($imgs as $k = = $v) {
if (!empty ($v)) {
File_put_contents ($this->imagessavepath[$k], $v);
}
}
}
/**
* Multi-threaded download images via URLs
* @param array $urls
* @return Array
*/
Private Function Multidownbyurls ($urls)
{
$opts = Array (
Curlopt_returntransfer = 1,
Curlopt_autoreferer = 1,
Curlopt_header = 0,
Curlopt_followlocation = 1,
Curlopt_useragent = ' mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1750.154 safari/537.36 '
);
$mutil = new Multihttprequest ($urls, $opts);
$mutil->start ();
return $mutil->getres ();
}
}
$url = ' http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gbk& word=%c3%c0%c5%ae%cd%bc&fr=ala&ala=1&alatpl=cover&pos=0#z=0&pn=&ic=0&st=-1& Face=0&s=0&lm=-1 ';
$savepath = __dir__. Directory_separator. ' Images '. Directory_separator;
$HD = new Hdbutifulygril ($url, $savepath);
for ($i = 1; $i < $i + +) {
$HD->start ($i);
}
Curl Multithreading Download Class