|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: User |
|
5
|
|
|
* Date: 8/13/2019 |
|
6
|
|
|
* Time: 10:33 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace crocodicstudio\crudbooster\helpers; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class CurlHelper |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $url; |
|
16
|
|
|
private $headers; |
|
17
|
|
|
private $data; |
|
18
|
|
|
private $type; |
|
19
|
|
|
private $timeout; |
|
20
|
|
|
private $user_agent; |
|
21
|
|
|
private $referer; |
|
22
|
|
|
private $cookie; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct($url, $type = "POST") |
|
25
|
|
|
{ |
|
26
|
|
|
$this->url = $url; |
|
27
|
|
|
$this->type = $type; |
|
28
|
|
|
$this->timeout = 30; |
|
29
|
|
|
$this->user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'; |
|
30
|
|
|
$this->referer = url("/"); |
|
31
|
|
|
$this->cookie = false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function cookie($enable) { |
|
35
|
|
|
$this->cookie = $enable; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function referer($referer) { |
|
39
|
|
|
$this->referer = $referer; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function userAgent($user_agent) { |
|
43
|
|
|
$this->user_agent = $user_agent; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function timeout($seconds) { |
|
47
|
|
|
$this->timeout = $seconds; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function headers(array $headers) { |
|
51
|
|
|
$newHeaders = []; |
|
52
|
|
|
foreach($headers as $key=>$val) { |
|
53
|
|
|
$newHeaders[] = $key.": ".$val; |
|
54
|
|
|
} |
|
55
|
|
|
$this->headers = $newHeaders; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function data(array $data) { |
|
59
|
|
|
$this->data = $data; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function send() { |
|
63
|
|
|
$ch = curl_init($this->url); |
|
64
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->type); |
|
|
|
|
|
|
65
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
66
|
|
|
curl_setopt($ch, CURLOPT_MAXREDIRS, 5); |
|
67
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); |
|
68
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
69
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->data); |
|
70
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); |
|
71
|
|
|
curl_setopt($ch,CURLOPT_USERAGENT,$this->user_agent); |
|
72
|
|
|
curl_setopt($ch, CURLOPT_REFERER, $this->referer); |
|
73
|
|
|
if($this->cookie === true) { |
|
74
|
|
|
$cookie_dir = storage_path("cookies"); |
|
75
|
|
|
if(!file_exists($cookie_dir)) mkdir($cookie_dir); |
|
76
|
|
|
$cookie_file = $cookie_dir."/".md5(request()->ip()).".txt"; |
|
77
|
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); |
|
78
|
|
|
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$response = curl_exec($ch); |
|
|
|
|
|
|
82
|
|
|
$err = curl_error($ch); |
|
|
|
|
|
|
83
|
|
|
curl_close($ch); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
if($err) { |
|
86
|
|
|
return $err; |
|
87
|
|
|
}else { |
|
88
|
|
|
return $response; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |