-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPage.php
More file actions
109 lines (95 loc) · 2.24 KB
/
Copy pathPage.php
File metadata and controls
109 lines (95 loc) · 2.24 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* @package maximalist\GetImages
*/
namespace maximalist\GetImages;
/**
* Downloads a HTML document and extracts hyperlinks and image URLs
*
* @since 0.2
*
* @property string $url Page URL
* @property string $host Page host
* @property array $links Collection of found pages
* @property array $images Collection of found images
*
* @method string absUrl( string $url )
* @method array getLinks()
* @method array getImages()
*
* @uses DOMDocument to parse HTML
*
*/
class Page {
private $url;
private $host;
private $links = [];
private $images = [];
function __construct( string $url ) {
if( filter_var( $url, FILTER_VALIDATE_URL ) === false )
throw new \Exception( "Invalid URL" );
$this->url = $url;
$url = parse_url( $url );
$this->host = $url['scheme'].'://'.$url['host'];
if( !empty( $url['port'] ) )
$this->host .= ':'.$url['port'];
}
/**
* Collect links to other pages and images
*/
function parse() {
$html = file_get_contents( $this->url );
if( $html === false )
throw new \Exception( "Can't load ".$this->url );
$dom = new \DOMDocument();
libxml_use_internal_errors( true );
$dom->loadHTML( $html );
libxml_clear_errors();
$links = $dom->getElementsByTagName( 'img' );
foreach( $links as $link )
{
$src = $this->absUrl( $link->getAttribute( 'src' ) );
$this->images[$src] = '';
}
$links = $dom->getElementsByTagName( 'a' );
foreach( $links as $link )
{
$href = $this->absUrl( $link->getAttribute( 'href' ) );
$this->links[$href] = '';
}
}
/**
* Make link absolute
*
* @param string $url Link URL
* @return string Absolute link
*/
function absUrl( string $url ) {
$url = parse_url( $url );
$s = '';
if( !empty( $url['scheme'] ) )
$s .= $url['scheme'].'://';
$s .= !empty( $url['host'] ) ? $url['host'] : $this->host;
if( !empty( $url['port'] ) )
$s .= ':'.$url['port'];
if( !empty( $url['path'] ) )
$s .= $url['path'];
return $s;
}
/**
* Return collection of found page links
*
* @return array Links collection
*/
function getLinks() {
return array_keys( $this->links );
}
/**
* Return collection of found images
*
* @return array Images collection
*/
function getImages() {
return array_keys( $this->images );
}
}