-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathfetch_url.class.php
93 lines (81 loc) · 2.33 KB
/
fetch_url.class.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
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
<?php
/**
* Fetch URL class
*
* Fetch a page by url
*
* @author Han Lin Yap < http://zencodez.net/ >
* @copyright 2011 zencodez.net
* @license http://creativecommons.org/licenses/by-sa/3.0/
* @package fetch_url
* @version 1.2 - 2011-06-18
*/
class Fetch_url {
public $header;
public $source;
public $error;
function __construct($url, $posts=null, $save_cookie_path=null, $new_session=null, $user_agent=null, $port=null, $auth=null) {
// nollställer header
$this->header = '';
$this->error = '';
if ($save_cookie_path) {
// Skapar en cookie fil om den inte existerar
if (!file_exists($save_cookie_path)) {
$handle = @fopen($save_cookie_path, 'w');
if (!$handle) {
$this->error = 'Cannot create cookie file';
return;
}
fclose($handle);
chmod($save_cookie_path, 0777);
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // set url to get
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 9); // times out after 9s
curl_setopt($ch, CURLOPT_HEADER, 0); // output header
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(__class__,'header_callback'));
if ($user_agent) {
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
}
if ($new_session) {
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
}
if ($port) {
curl_setopt($ch, CURLOPT_PORT, $port);
}
if ($auth) {
curl_setopt($ch, CURLOPT_USERPWD, $auth[0] . ":" . $auth[1]);
}
// if $_POST
if ($posts) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $posts);
}
if ($save_cookie_path) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $save_cookie_path); // spara
curl_setopt($ch, CURLOPT_COOKIEFILE, $save_cookie_path); // hämta
}
$this->source = curl_exec($ch);
if ($curl_error = curl_error($ch)) {
$this->error = 'CURL error : ' . $curl_error;
return;
}
if ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
$this->error = 'HTTP code is ' . $http_code;
}
curl_close($ch);
}
function header_callback($ch, $data) {
$this->header .= $data;
return strlen($data);
}
public function get_header($type) {
if (!strstr($this->header,$type)) return false;
$header = nl2br(strstr($this->header,$type));
$header = explode("<br />",$header);
return $header[0];
}
}
?>