|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package org.openpsa.httplib |
|
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
|
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Helpers for HTTP content fetching and handling |
|
13
|
|
|
* |
|
14
|
|
|
* @package org.openpsa.httplib |
|
15
|
|
|
*/ |
|
16
|
|
|
class org_openpsa_httplib_helpers extends midcom_baseclasses_components_purecode |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Get value of a meta tag in HTML page. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $html HTML to parse |
|
22
|
|
|
* @param string $name Name of the meta tag to fetch |
|
23
|
|
|
* @return ?string |
|
24
|
|
|
*/ |
|
25
|
2 |
|
public static function get_meta_value($html, $name) |
|
26
|
|
|
{ |
|
27
|
2 |
|
$crawler = new Crawler($html); |
|
28
|
2 |
|
if ($crawler->filter('head meta[name="' . $name . '"]')->count() == 0) { |
|
29
|
1 |
|
return null; |
|
30
|
|
|
} |
|
31
|
1 |
|
return (string) $crawler->filter('head meta[name="' . $name . '"]')->attr('content'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get value(s) of a link tag(s) in HTML page. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $html HTML to parse |
|
38
|
|
|
* @param string $relation Relation (rel) or reverse relation (rev) of the link tag to fetch |
|
39
|
|
|
* @return array Links matching given criteria as arrays containing keys title, href and optionally hreflang |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public static function get_link_values($html, $relation) : array |
|
42
|
|
|
{ |
|
43
|
1 |
|
$crawler = new Crawler($html); |
|
44
|
1 |
|
$nodes = $crawler->filter('head link[rel="' . $relation . '"]'); |
|
45
|
|
|
|
|
46
|
|
|
return $nodes->each(function(Crawler $node, $i) { |
|
47
|
1 |
|
$tag = ['title' => false, 'href' => false, 'hreflang' => false]; |
|
48
|
1 |
|
foreach ($tag as $property => &$value) { |
|
49
|
1 |
|
if ($node->attr($property) !== null) { |
|
50
|
1 |
|
$value = $node->attr($property); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
1 |
|
return $tag; |
|
54
|
1 |
|
}); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get value(s) of an anchor tag(s) in HTML page. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $html HTML to parse |
|
61
|
|
|
* @param string $relation Relation (rel) of the anchor to fetch |
|
62
|
|
|
* @return array Links matching given criteria as arrays containing keys title, href and value |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public static function get_anchor_values($html, $relation) : array |
|
65
|
|
|
{ |
|
66
|
2 |
|
$crawler = new Crawler($html); |
|
67
|
2 |
|
$nodes = $crawler->filter('a[rel="' . $relation . '"]'); |
|
68
|
|
|
|
|
69
|
|
|
return $nodes->each(function(Crawler $node, $i) { |
|
70
|
|
|
return [ |
|
71
|
1 |
|
'title' => ($node->attr('title') !== null) ? $node->attr('title') : false, |
|
72
|
1 |
|
'href' => ($node->attr('href') !== null) ? $node->attr('href') : false, |
|
73
|
1 |
|
'value' => ($node->text() !== null) ? $node->text() : false, |
|
|
|
|
|
|
74
|
|
|
]; |
|
75
|
2 |
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|