1
+ <?php if ( ! defined ('BASEPATH ' )) exit ('No direct script access allowed ' );
2
+ /**
3
+ * @author Philip Sturgeon
4
+ * @created 9 Dec 2008
5
+ */
6
+
7
+ class cURL {
8
+
9
+ private $ CI ; // CodeIgniter instance
10
+
11
+ private $ session ; // Contains the cURL handler for a session
12
+ private $ url ; // URL of the session
13
+ private $ options = array (); // Populates curl_setopt_array
14
+
15
+ public $ error_code ; // Error code returned as an int
16
+ public $ error_string ; // Error message returned as a string
17
+ public $ info ; // Returned after request (elapsed time, etc)
18
+
19
+ function __construct ($ url = '' ) {
20
+ $ this ->CI =& get_instance ();
21
+ log_message ('debug ' , 'cURL Class Initialized ' );
22
+
23
+ if (!function_exists ('curl_init ' )) {
24
+ log_message ('error ' , 'cURL Class - PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL. ' ) ;
25
+ }
26
+
27
+ if ($ url ) $ this ->create ($ url );
28
+ }
29
+
30
+ /* =================================================================================
31
+ * SIMPLE METHODS
32
+ * Using these methods you can make a quick and easy cURL call with one line.
33
+ * ================================================================================= */
34
+
35
+ // Return a get request results
36
+ public function simple_get ($ url , $ options = array ()) {
37
+
38
+ // If a URL is provided, create new session
39
+ $ this ->create ($ url );
40
+
41
+ // Add in the specific options provided
42
+ $ this ->options ($ options );
43
+
44
+ return $ this ->execute ();
45
+ }
46
+
47
+ // Send a post request on its way with optional parameters (and get output)
48
+ // $url = '', $params = array(), $options = array()
49
+ public function simple_post ($ url , $ params = array (), $ options = array ()) {
50
+ $ this ->create ($ url );
51
+
52
+ $ this ->post ($ params , $ options );
53
+
54
+ return $ this ->execute ();
55
+ }
56
+
57
+ public function simple_ftp_get ($ url , $ file_path , $ username = '' , $ password = '' ) {
58
+
59
+ // If there is no ftp:// or any protocol entered, add ftp://
60
+ if (!preg_match ('!^(ftp|sftp)://! i ' , $ url )) {
61
+ $ url = 'ftp:// ' .$ url ;
62
+ }
63
+
64
+ // Use an FTP login
65
+ if ($ username != '' )
66
+ {
67
+ $ auth_string = $ username ;
68
+
69
+ if ($ password != '' )
70
+ {
71
+ $ auth_string .= ': ' .$ password ;
72
+ }
73
+
74
+ // Add the user auth string after the protocol
75
+ $ url = str_replace (':// ' , ':// ' .$ auth_string .'@ ' , $ url );
76
+ }
77
+
78
+ // Add the filepath
79
+ $ url .= $ file_path ;
80
+
81
+ $ this ->options (CURLOPT_BINARYTRANSFER , TRUE );
82
+ $ this ->options (CURLOPT_VERBOSE , TRUE );
83
+
84
+ return $ this ->execute ();
85
+ }
86
+
87
+ /* =================================================================================
88
+ * ADVANCED METHODS
89
+ * Use these methods to build up more complex queries
90
+ * ================================================================================= */
91
+
92
+ public function post ($ params = array (), $ options = array ()) {
93
+
94
+ // If its an array (instead of a query string) then format it correctly
95
+ if (is_array ($ params )) {
96
+ $ params = http_build_query ($ params );
97
+ }
98
+
99
+ // Add in the specific options provided
100
+ $ this ->options ($ options );
101
+
102
+ $ this ->option (CURLOPT_POST , TRUE );
103
+ $ this ->option (CURLOPT_POSTFIELDS , $ params );
104
+ }
105
+
106
+ public function set_cookies ($ params = array ()) {
107
+
108
+ if (is_array ($ params )) {
109
+ $ params = http_build_query ($ params );
110
+ }
111
+
112
+ $ this ->option (CURLOPT_COOKIE , $ params );
113
+ return $ this ;
114
+ }
115
+
116
+ public function http_login ($ username = '' , $ password = '' ) {
117
+ $ this ->option (CURLOPT_USERPWD , $ username .': ' .$ password );
118
+ return $ this ;
119
+ }
120
+
121
+ public function proxy ($ url = '' , $ port = 80 ) {
122
+
123
+ $ this ->option (CURLOPT_HTTPPROXYTUNNEL . TRUE );
124
+ $ this ->option (CURLOPT_PROXY , $ url .': ' . 80 );
125
+ return $ this ;
126
+ }
127
+
128
+ public function proxy_login ($ username = '' , $ password = '' ) {
129
+ $ this ->option (CURLOPT_PROXYUSERPWD , $ username .': ' .$ password );
130
+ return $ this ;
131
+ }
132
+
133
+ public function options ($ options = array ()) {
134
+
135
+ // Merge options in with the rest - done as array_merge() does not overwrite numeric keys
136
+ foreach ($ options as $ option_code => $ option_value ) {
137
+ $ this ->option ($ option_code , $ option_value );
138
+ }
139
+ unset($ option_code , $ option_value );
140
+
141
+ // Set all options provided
142
+ curl_setopt_array ($ this ->session , $ this ->options );
143
+
144
+ return $ this ;
145
+ }
146
+
147
+ public function option ($ code , $ value ) {
148
+ $ this ->options [$ code ] = $ value ;
149
+ return $ this ;
150
+ }
151
+
152
+ // Start a session from a URL
153
+ public function create ($ url ) {
154
+
155
+ // Reset the class
156
+ $ this ->set_defaults ();
157
+
158
+ // If no a protocol in URL, assume its a CI link
159
+ if (!preg_match ('!^\w+://! i ' , $ url )) {
160
+ $ this ->CI ->load ->helper ('url ' );
161
+ $ url = site_url ($ url );
162
+ }
163
+
164
+ $ this ->url = $ url ;
165
+ $ this ->session = curl_init ($ this ->url );
166
+
167
+ return $ this ;
168
+ }
169
+
170
+ // End a session and return the results
171
+ public function execute () {
172
+
173
+ // Set two default options, and merge any extra ones in
174
+ if (!isset ($ this ->options [CURLOPT_TIMEOUT ])) $ this ->options [CURLOPT_TIMEOUT ] = 30 ;
175
+ if (!isset ($ this ->options [CURLOPT_RETURNTRANSFER ])) $ this ->options [CURLOPT_RETURNTRANSFER ] = TRUE ;
176
+ if (!isset ($ this ->options [CURLOPT_FOLLOWLOCATION ])) $ this ->options [CURLOPT_FOLLOWLOCATION ] = TRUE ;
177
+ if (!isset ($ this ->options [CURLOPT_FAILONERROR ])) $ this ->options [CURLOPT_FAILONERROR ] = TRUE ;
178
+
179
+ $ this ->options ();
180
+
181
+ // Execute the request
182
+ $ return = curl_exec ($ this ->session );
183
+
184
+ // Request failed
185
+ if ($ return === FALSE )
186
+ {
187
+ $ this ->error_code = curl_errno ($ this ->session );
188
+ $ this ->error_string = curl_error ($ this ->session );
189
+
190
+ curl_close ($ this ->session );
191
+ $ this ->session = NULL ;
192
+ return FALSE ;
193
+
194
+ // Request successful
195
+ } else {
196
+
197
+ $ this ->info = curl_getinfo ($ this ->session );
198
+
199
+ curl_close ($ this ->session );
200
+ $ this ->session = NULL ;
201
+ return $ return ;
202
+ }
203
+
204
+ }
205
+
206
+ private function set_defaults () {
207
+ $ this ->info = array ();
208
+ $ this ->options = array ();
209
+ $ this ->error_code = 0 ;
210
+ $ this ->error_string = '' ;
211
+ }
212
+ }
213
+ // END cURL Class
214
+
215
+ /* End of file cURL.php */
216
+ /* Location: ./application/libraries/cURL.php */
217
+ ?>
0 commit comments