1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Sergey Glagolev <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
* @package frontend.share
|
8
|
|
|
*
|
9
|
|
|
* Компонент
|
10
|
|
|
*
|
11
|
|
|
* <pre>
|
12
|
|
|
* $curl = new Curl(array(
|
13
|
|
|
* 'TimeOut' => $this->time_out,
|
14
|
|
|
* 'Proxy' => 'localhost:3128'
|
15
|
|
|
* );
|
16
|
|
|
* </pre>
|
17
|
|
|
*/
|
18
|
|
|
class Curl extends CApplicationComponent
|
19
|
|
|
{
|
20
|
|
|
public $result;
|
21
|
|
|
|
22
|
|
|
private $ch;
|
23
|
|
|
|
24
|
|
|
private $error;
|
25
|
|
|
|
26
|
|
|
public function __construct(array $options = array())
|
27
|
|
|
{
|
28
|
|
|
if( !extension_loaded('curl') )
|
29
|
|
|
throw new Exception('Curl module are not installed!');
|
30
|
|
|
|
31
|
|
|
$this->ch = curl_init();
|
32
|
|
|
curl_setopt($this->ch, CURLOPT_HEADER, (isset($options['header'])) ? true : false);
|
33
|
|
|
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
|
34
|
|
|
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
|
35
|
|
|
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Accept: text/plain', 'User-Agent: cURL'));
|
36
|
|
|
|
37
|
|
|
foreach($options as $key => $value)
|
38
|
|
|
if( method_exists($this, "set".$key) )
|
39
|
|
|
call_user_func_array(array($this, "set".$key), array($value));
|
40
|
|
|
}
|
41
|
|
|
|
42
|
|
|
public function init()
|
43
|
|
|
{
|
44
|
|
|
|
45
|
|
|
}
|
46
|
|
|
|
47
|
|
|
/**
|
48
|
|
|
* @param integer $timeOut
|
49
|
|
|
*/
|
50
|
|
|
public function setTimeOut($timeOut = 30)
|
51
|
|
|
{
|
52
|
|
|
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $timeOut);
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* @param string $proxy localhost:3128
|
57
|
|
|
*/
|
58
|
|
|
public function setProxy($proxy)
|
59
|
|
|
{
|
60
|
|
|
curl_setopt($this->ch, CURLOPT_PROXY, $proxy);
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
/**
|
64
|
|
|
* @param integer $flag
|
65
|
|
|
*/
|
66
|
|
|
public function setFollowLocation($flag)
|
67
|
|
|
{
|
68
|
|
|
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, $flag);
|
69
|
|
|
}
|
70
|
|
|
|
71
|
|
|
/**
|
72
|
|
|
* @param string $agent
|
73
|
|
|
*/
|
74
|
|
|
public function setUserAgent($agent)
|
75
|
|
|
{
|
76
|
|
|
curl_setopt($this->ch, CURLOPT_USERAGENT, $agent);
|
77
|
|
|
}
|
78
|
|
|
|
79
|
|
|
/**
|
80
|
|
|
* GET request
|
81
|
|
|
*
|
82
|
|
|
* @param string $url
|
83
|
|
|
* @param array $getParams
|
84
|
|
|
*
|
85
|
|
|
* @return mixed
|
86
|
|
|
*/
|
87
|
|
|
public function get($url, $getParams = array())
|
88
|
|
|
{
|
89
|
|
|
$getParams = http_build_query($getParams);
|
90
|
|
|
$getParams = (preg_match("/\?/", $url) ? "&" : "?").$getParams;
|
91
|
|
|
|
92
|
|
|
curl_setopt($this->ch, CURLOPT_POST, false);
|
93
|
|
|
curl_setopt($this->ch, CURLOPT_URL, $url.$getParams);
|
94
|
|
|
|
95
|
|
|
return $this->exec();
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
* POST request
|
100
|
|
|
*
|
101
|
|
|
* @param string $url
|
102
|
|
|
* @param array $postParams
|
103
|
|
|
*
|
104
|
|
|
* @return mixed
|
105
|
|
|
*/
|
106
|
|
|
public function post($url, $postParams = array())
|
107
|
|
|
{
|
108
|
|
|
curl_setopt($this->ch, CURLOPT_URL, $url);
|
109
|
|
|
curl_setopt($this->ch, CURLOPT_POST, true);
|
110
|
|
|
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postParams);
|
111
|
|
|
|
112
|
|
|
return $this->exec();
|
113
|
|
|
}
|
114
|
|
|
|
115
|
|
|
private function exec()
|
116
|
|
|
{
|
117
|
|
|
$this->result = curl_exec($this->ch);
|
118
|
|
|
$this->error = curl_error($this->ch);
|
119
|
|
|
|
120
|
|
|
return $this->result;
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
/**
|
124
|
|
|
* @return string error
|
125
|
|
|
*
|
126
|
|
|
*/
|
127
|
|
|
public function getLastError()
|
128
|
|
|
{
|
129
|
|
|
return $this->error;
|
130
|
|
|
}
|
131
|
|
|
|
132
|
|
|
public function __destruct()
|
133
|
|
|
{
|
134
|
|
|
curl_close($this->ch);
|
135
|
|
|
}
|
136
|
|
|
} |