-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurlExecution.php
More file actions
executable file
·82 lines (64 loc) · 2.04 KB
/
CurlExecution.php
File metadata and controls
executable file
·82 lines (64 loc) · 2.04 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
<?php
/**
* Created by PhpStorm.
* User: Alessandra Zullo
* Date: 06/02/2016
* Time: 12.00
*/
class CurlExecution
{
public $packtPage;
function CurlExecution($url){
$this->packtPage = $this->curl($url);
}
public function curl($url) {
try {
$ch = curl_init();
if (FALSE === $ch)
throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/html'
));
$content = curl_exec($ch);
if (FALSE === $content || NULL == $content)
throw new Exception(curl_error($ch), curl_errno($ch));
return $content;
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
}
}
// Defining the XPATHObject
public function returnXPathObject($item) {
$xmlPageDom = new DomDocument();
@$xmlPageDom->loadHTML($item);
$xmlPageXPath = new DOMXPath($xmlPageDom);
return $xmlPageXPath;
}
//Scraping page with xpath -> XPExpression: xpath expression
public function returnData($XPExpression){
$PageXpath = $this->returnXPathObject($this->packtPage);
$data=$PageXpath->query($XPExpression);
return $data;
}
public function useXPathOnDOMNode(DOMNode $element, $xPath){
$xPathObj = $this->returnXPathObject($this->DOMinnerHTML($element));
$data=$xPathObj->query($xPath);
return $data;
}
function DOMinnerHTML(DOMNode $element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
}