| 10582 |
lgm |
1 |
<?php defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
**/
|
|
|
5 |
|
|
|
6 |
/**
|
|
|
7 |
* CodeIgniter MCurl Class
|
|
|
8 |
*
|
|
|
9 |
* Work with remote servers via multi-cURL much easier than using the native PHP bindings.
|
|
|
10 |
*
|
|
|
11 |
* @package CodeIgniter
|
|
|
12 |
* @subpackage Libraries
|
|
|
13 |
* @category Libraries
|
|
|
14 |
* @author Chad Hutchins
|
|
|
15 |
* @link http://github.com/chadhutchins/codeigniter-mcurl
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
class Mcurl {
|
|
|
19 |
|
|
|
20 |
private $_ci; // CodeIgniter instance
|
|
|
21 |
private $calls = array(); // multidimensional array that holds individual calls and data
|
|
|
22 |
private $curl_parent; // the curl multi handle resource
|
|
|
23 |
|
|
|
24 |
function __construct()
|
|
|
25 |
{
|
|
|
26 |
$this->_ci = & get_instance();
|
|
|
27 |
log_message('debug', 'Mcurl Class Initialized');
|
|
|
28 |
if (!$this->is_enabled())
|
|
|
29 |
{
|
|
|
30 |
log_message('error', 'Mcurl Class - PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.');
|
|
|
31 |
}
|
|
|
32 |
else
|
|
|
33 |
{
|
|
|
34 |
$this->curl_parent = curl_multi_init();
|
|
|
35 |
}
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
// check to see if necessary function exists
|
|
|
39 |
function is_enabled()
|
|
|
40 |
{
|
|
|
41 |
return function_exists('curl_multi_init');
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
// method to add curl requests to the multi request queue
|
|
|
45 |
function add_call($key=null, $method, $url, $params = array(), $options = array())
|
|
|
46 |
{
|
|
|
47 |
if (is_null($key))
|
|
|
48 |
{
|
|
|
49 |
$key = count($this->calls);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
// check to see if the multi handle has been closed
|
|
|
53 |
// init the multi handle again
|
|
|
54 |
$resource_type = get_resource_type($this->curl_parent);
|
|
|
55 |
if(!$resource_type || $resource_type == 'Unknown')
|
|
|
56 |
{
|
|
|
57 |
$this->calls = array();
|
|
|
58 |
$this->curl_parent = curl_multi_init();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$this->calls [$key]= array(
|
|
|
62 |
"method" => $method,
|
|
|
63 |
"url" => $url,
|
|
|
64 |
"params" => $params,
|
|
|
65 |
"options" => $options,
|
|
|
66 |
"curl" => null,
|
|
|
67 |
"response" => null,
|
|
|
68 |
"error" => null
|
|
|
69 |
);
|
|
|
70 |
|
|
|
71 |
$this->calls[$key]["curl"] = curl_init();
|
|
|
72 |
|
|
|
73 |
$newdata =$params;
|
|
|
74 |
// If its an array (instead of a query string) then format it correctly
|
|
|
75 |
if (is_array($params))
|
|
|
76 |
{
|
|
|
77 |
$params = http_build_query($params, NULL, '&');
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$method = strtoupper($method);
|
|
|
81 |
|
|
|
82 |
// only supports get/post requests
|
|
|
83 |
// set some special curl opts for each type of request
|
|
|
84 |
switch ($method)
|
|
|
85 |
{
|
|
|
86 |
case "POST":
|
|
|
87 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_URL, $url);
|
|
|
88 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_POST, TRUE);
|
|
|
89 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_POSTFIELDS, $params);
|
|
|
90 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_SSL_VERIFYPEER, false);
|
|
|
91 |
break;
|
|
|
92 |
|
|
|
93 |
case "GET":
|
|
|
94 |
if (strpos($url,'?') !== false) {
|
|
|
95 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_URL, $url."&".$params);
|
|
|
96 |
}
|
|
|
97 |
else{
|
|
|
98 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_URL, $url."?".$params);
|
|
|
99 |
}
|
|
|
100 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_SSL_VERIFYPEER, false);
|
|
|
101 |
break;
|
|
|
102 |
|
|
|
103 |
default:
|
|
|
104 |
log_message('error', 'Mcurl Class - Provided http method is not supported. Only POST and GET are currently supported.');
|
|
|
105 |
break;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_RETURNTRANSFER, TRUE);
|
|
|
109 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_FOLLOWLOCATION, TRUE);
|
|
|
110 |
|
|
|
111 |
curl_setopt_array($this->calls[$key]["curl"], $options);
|
|
|
112 |
|
|
|
113 |
curl_multi_add_handle($this->curl_parent,$this->calls[$key]["curl"]);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
// run the calls in the curl requests in $this->calls
|
|
|
117 |
function execute()
|
|
|
118 |
{
|
|
|
119 |
if (count($this->calls))
|
|
|
120 |
{
|
|
|
121 |
// kick off the requests
|
|
|
122 |
do
|
|
|
123 |
{
|
|
|
124 |
$multi_exec_handle = curl_multi_exec($this->curl_parent,$active);
|
|
|
125 |
} while ($active>0);
|
|
|
126 |
|
|
|
127 |
// after all requests finish, set errors and repsonses in $this->calls
|
|
|
128 |
foreach ($this->calls as $key => $call)
|
|
|
129 |
{
|
|
|
130 |
$error = curl_error($this->calls[$key]["curl"]);
|
| 10739 |
lgm |
131 |
//print_r($key);
|
|
|
132 |
//print_r($error);
|
| 10582 |
lgm |
133 |
if (!empty($error))
|
|
|
134 |
{
|
| 10739 |
lgm |
135 |
if($key != 'recommended_accessories'){
|
| 10582 |
lgm |
136 |
$this->calls[$key]["error"] = $error;
|
| 10739 |
lgm |
137 |
$errmsg ='<h3>OH SNAP!!!! :</h3><p>Something is not right. <br/><h1>But Relax</h1> <br/>We are working hard on it. </p>';
|
|
|
138 |
//$this->write_log_send_mail('error',$msg);
|
|
|
139 |
//$this->session->set_flashdata('errormsg',$errmsg);
|
| 10802 |
lgm |
140 |
//redirect(base_url().'error_page/index');
|
|
|
141 |
//exit();
|
| 10739 |
lgm |
142 |
}
|
| 10582 |
lgm |
143 |
}
|
|
|
144 |
$this->calls[$key]["response"] = curl_multi_getcontent($this->calls[$key]["curl"]);
|
| 10800 |
lgm |
145 |
$http_status = curl_getinfo($this->calls[$key]["curl"], CURLINFO_HTTP_CODE);
|
| 10802 |
lgm |
146 |
if($key != 'recommended_accessories'){
|
|
|
147 |
if($http_status != 200) {
|
|
|
148 |
redirect(base_url().'error_page/index');
|
|
|
149 |
exit();
|
|
|
150 |
}
|
| 10800 |
lgm |
151 |
}
|
| 10756 |
lgm |
152 |
curl_multi_remove_handle($this->curl_parent,$this->calls[$key]["curl"]);
|
|
|
153 |
// if($key != 'recommended_accessories'){
|
|
|
154 |
// if(isset($this->calls[$key]["response"]) && !empty($this->calls[$key]["response"])){
|
|
|
155 |
// curl_multi_remove_handle($this->curl_parent,$this->calls[$key]["curl"]);
|
|
|
156 |
// }else{
|
|
|
157 |
// $this->calls[$key]["error"] = $error;
|
|
|
158 |
// $errmsg ='<h3>OH SNAP!!!! :</h3><p>Something is not right. <br/><h1>But Relax</h1> <br/>We are working hard on it. </p>';
|
|
|
159 |
// //$this->write_log_send_mail('error',$msg);
|
|
|
160 |
// //$this->session->set_flashdata('errormsg',$errmsg);
|
|
|
161 |
// redirect(base_url().'error_page/index',$data);
|
|
|
162 |
// exit();
|
|
|
163 |
// }
|
|
|
164 |
// }
|
| 10582 |
lgm |
165 |
}
|
|
|
166 |
|
|
|
167 |
curl_multi_close($this->curl_parent);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
return $this->calls;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
function debug()
|
|
|
174 |
{
|
|
|
175 |
echo "<h2>mcurl debug</h2>";
|
|
|
176 |
foreach ($this->calls as $call)
|
|
|
177 |
{
|
|
|
178 |
echo '<p>url: <b>'.$call["url"].'</b></p>';
|
|
|
179 |
if (!is_null($call["error"]))
|
|
|
180 |
{
|
|
|
181 |
echo '<p style="color:red;">error: <b>'.$call["error"].'</b></p>';
|
|
|
182 |
}
|
|
|
183 |
echo '<textarea cols="100" rows="10">'.htmlentities($call["response"])."</textarea><hr>";
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/* End of file mcurl.php */
|
|
|
190 |
/* Location: ./application/libraries/mcurl.php */
|