This repository has been archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSimpleCurlConnection.php
87 lines (71 loc) · 2.84 KB
/
SimpleCurlConnection.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
<?php
class SimpleCurlConnection{
private $data = "";
private $headers = array();
const USER_AGENT = "VLC/2.2.2 LibVLC/2.2.2";
private function fn_CURLOPT_HEADERFUNCTION($ch, $str){
$len = strlen($str);
$itemArr = explode(":",$str,2);
if (count($this->headers) == 0){
$this->headers[0] = trim($str);
}else if (count($itemArr) == 2){
$this->headers[$itemArr[0]] = trim($itemArr[1]);
}
return $len;
}
private function writefn($ch, $chunk) {
static $limit = 4096;
$len = strlen($this->data) + strlen($chunk);
if ($len >= $limit ) {
$this->data .= substr($chunk, 0, $limit-strlen($this->data));
return -1;
}
$this->data .= $chunk;
return strlen($chunk);
}
public function get_headers_curl($url){
// erzeuge einen neuen cURL-Handle
$ch = curl_init();
// setze die URL und andere Optionen
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
// curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, "fn_CURLOPT_HEADERFUNCTION")); // handle received headers
curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
// curl_setopt($ch, CURLOPT_WRITEFUNCTION, "writefn");
// curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'fn_CURLOPT_WRITEFUNCTION'); // callad every CURLOPT_BUFFERSIZE
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); // more progress info
// führe die Aktion aus und gib die Daten an den Browser weiter
$this->data = "";
$this->headers = array();
$result = curl_exec($ch);
// schließe den cURL-Handle und gib die Systemresourcen frei
curl_close($ch);
return $this->headers;
}
public function file_get_contents_curl($url) {
$this->data = "";
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, "writefn"));
curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
if (!curl_exec($ch)){
$this->data = false;
}
curl_close($ch);
return $this->data;
}
}
?>