Repository navigation
Expand file tree
/
Copy pathindex.php
More file actions
84 lines (53 loc) · 1.57 KB
/
Copy pathindex.php
File metadata and controls
84 lines (53 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
require_once __DIR__.'/vendor/autoload.php';
use GuzzleHttp\Psr7\Response;
use QL\QueryList;
use GuzzleHttp\Client;
define("DS", DIRECTORY_SEPARATOR);
define("publicDir", __DIR__.DS.'public');
define("htmlDir", publicDir.DS.'html');
define("imageDir", publicDir.DS.'image');
$baseUrl = "http://www.1fang.wang";
//$client = new Client(['base_uri' => $baseUrl]);
//
//
//$result = $client->get(
// '/xinfang/', [
// RequestOptions::SINK => htmlDir.'/xinfang.html'
// ]
//);
//
//
//if (file_exists(htmlDir.'/xinfang.html')) {
// echo "下载成功";
//}
$html = file_get_contents(htmlDir.'/xinfang.html');
$urls = QueryList::html($html)->find('img')->attrs('src')->map(
function ($item) use ($baseUrl) { return $baseUrl.$item; }
)->toArray();
QueryList::multiGet($urls)->success(
function (QueryList $ql, Response $response, $index) use ($urls, $baseUrl) {
$filename = imageDir.DS.handleImageName($baseUrl, $urls[$index]);
if ( ! file_exists($filename)) {
$result = handleBody($response, $filename);
if ($result) {
echo "下载成功".'___'.$index.PHP_EOL;
}
}
}
)->send();
function handleImageName($baseUrl, $image)
{
$img = explode('/', \str_replace($baseUrl, '', $image));
return $img[count($img) - 1];
}
function handleBody(Response $response, $filename)
{
$fileHandle = fopen($filename, 'wb+');
$body = $response->getBody();
fwrite($fileHandle, $body);
$body->close();
fclose($fileHandle);
return true;
}
//var_dump($result);