-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXpath.php
52 lines (43 loc) · 1.16 KB
/
Xpath.php
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
<?php
class XPATH{
public $dom, $xpath;
//constructor
public function __construct($url){
$html = $this->_curl($url);
$this->dom = new DOMDocument();
@$this->dom->loadHTML($html);
$this->xpath = new DOMXpath($this->dom);
}
public function query($q){
$result = $this->xpath->query($q);
return $result;
}
public function preview($results){
echo "<prev>";
print_r($results);
echo "<br> Node Values <br/>";
foreach ($results as $result) {
echo trim($result->nodeValue) . '<br>';
$array[] = $result;
}
echo "<br><br>";
print_r($array);
}
private function _curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
if(!$result){
echo "<br /> cURL error number:" .curl_errno($ch);
echo "<br /> cURL error:" .curl_error($ch) . "on URL ." . $url;
var_dump(curl_getinfo($ch));
var_dump(curl_error($ch));
exit;
}
return $result;
}
}