Skip to content

Commit b6a363b

Browse files
author
Phil Sturgeon
committed
Initial import.
1 parent 00201e2 commit b6a363b

File tree

3 files changed

+334
-0
lines changed

3 files changed

+334
-0
lines changed

README

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
CodeIgniter-cURL
2+
===================
3+
4+
CodeIgniter-cURL is a CodeIgniter library which makes it easy to do simple cURL requests
5+
and makes more complicated cURL requests easier too.
6+
7+
Usage
8+
-----
9+
10+
echo $this->curl->simple_get('http://example.com');
11+
echo $this->curl->simple_post('curl_test/message', array('message'=>'Sup buddy'));
12+
13+
For more up to date usage and in-depth examples check the CodeIgniter wiki page:
14+
15+
http://codeigniter.com/wiki/Curl_library/
16+
17+
Extra
18+
-----
19+
20+
If you'd like to request changes, report bug fixes, or contact
21+
the developer of this library, email <[email protected]>

controllers/curl_test.php

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
class Curl_test extends Controller {
4+
5+
function __construct() {
6+
parent::Controller();
7+
$this->load->library('curl');
8+
}
9+
10+
function simple_get()
11+
{
12+
$responce = $this->curl->simple_get('curl_test/get_message');
13+
14+
echo '<h1>Simple Get</h1>';
15+
echo '<p>--------------------------------------------------------------------------</p>';
16+
17+
if($responce) {
18+
19+
echo $responce;
20+
21+
22+
echo '<br/><p>--------------------------------------------------------------------------</p>';
23+
echo '<h3>Debug</h3>';
24+
echo '<pre>';
25+
print_r($this->curl->info);
26+
echo '</pre>';
27+
28+
} else {
29+
echo '<strong>cURL Error</strong>: '.$this->curl->error_string;
30+
}
31+
}
32+
33+
function simple_post()
34+
{
35+
$responce = $this->curl->simple_post('curl_test/message', array('message'=>'Sup buddy'));
36+
37+
echo '<h1>Simple Post</h1>';
38+
echo '<p>--------------------------------------------------------------------------</p>';
39+
40+
if($responce) {
41+
42+
echo $responce;
43+
44+
45+
echo '<br/><p>--------------------------------------------------------------------------</p>';
46+
echo '<h3>Debug</h3>';
47+
echo '<pre>';
48+
print_r($this->curl->info);
49+
echo '</pre>';
50+
51+
} else {
52+
echo '<strong>cURL Error</strong>: '.$this->curl->error_string;
53+
}
54+
}
55+
56+
function message() {
57+
echo "<h2>Posted Message</h2>";
58+
echo $_POST['message'];
59+
}
60+
61+
function get_message() {
62+
echo "<h2>Get got!</h2>";
63+
}
64+
65+
function advance()
66+
{
67+
$this->curl->create('curl_test/cookies')
68+
->set_cookies(array('message'=>'Im advanced :-p'));
69+
70+
$responce = $this->curl->execute();
71+
72+
echo '<h1>Advanced</h1>';
73+
echo '<p>--------------------------------------------------------------------------</p>';
74+
75+
if($responce) {
76+
77+
echo $responce;
78+
79+
echo '<br/><p>--------------------------------------------------------------------------</p>';
80+
echo '<h3>Debug</h3>';
81+
echo '<pre>';
82+
print_r($this->curl->info);
83+
echo '</pre>';
84+
85+
} else {
86+
echo '<strong>cURL Error</strong>: '.$this->curl->error_string;
87+
}
88+
}
89+
90+
function cookies() {
91+
echo "<h2>Cookies</h2>";
92+
print_r($_COOKIE);
93+
}
94+
}
95+
96+
?>

libraries/curl.php

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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

Comments
 (0)