| 12694 |
anikendra |
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 |
|
|
|
27 |
$this->_ci = & get_instance();
|
|
|
28 |
log_message('debug', 'Mcurl Class Initialized');
|
|
|
29 |
if (!$this->is_enabled())
|
|
|
30 |
{
|
|
|
31 |
log_message('error', 'Mcurl Class - PHP was not built with cURL enabled. Rebuild PHP with --with-curl to use cURL.');
|
|
|
32 |
}
|
|
|
33 |
else
|
|
|
34 |
{
|
|
|
35 |
$this->curl_parent = curl_multi_init();
|
|
|
36 |
}
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
// check to see if necessary function exists
|
|
|
40 |
function is_enabled()
|
|
|
41 |
{
|
|
|
42 |
return function_exists('curl_multi_init');
|
|
|
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 |
error_log("URL = $url ".print_r($params,1));
|
|
|
48 |
if (is_null($key))
|
|
|
49 |
{
|
|
|
50 |
$key = count($this->calls);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
// check to see if the multi handle has been closed
|
|
|
54 |
// init the multi handle again
|
|
|
55 |
$resource_type = get_resource_type($this->curl_parent);
|
|
|
56 |
if(!$resource_type || $resource_type == 'Unknown')
|
|
|
57 |
{
|
|
|
58 |
$this->calls = array();
|
|
|
59 |
$this->curl_parent = curl_multi_init();
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
$this->calls [$key]= array(
|
|
|
63 |
"method" => $method,
|
|
|
64 |
"url" => $url,
|
|
|
65 |
"params" => $params,
|
|
|
66 |
"options" => $options,
|
|
|
67 |
"curl" => null,
|
|
|
68 |
"response" => null,
|
|
|
69 |
"error" => null
|
|
|
70 |
);
|
|
|
71 |
|
|
|
72 |
$this->calls[$key]["curl"] = curl_init();
|
|
|
73 |
|
|
|
74 |
$newdata =$params;
|
|
|
75 |
// If its an array (instead of a query string) then format it correctly
|
|
|
76 |
if (is_array($params))
|
|
|
77 |
{
|
|
|
78 |
$params = http_build_query($params, NULL, '&');
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
$method = strtoupper($method);
|
|
|
82 |
|
|
|
83 |
// only supports get/post requests
|
|
|
84 |
// set some special curl opts for each type of request
|
|
|
85 |
switch ($method)
|
|
|
86 |
{
|
|
|
87 |
case "POST":
|
|
|
88 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_URL, $url);
|
|
|
89 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_POST, TRUE);
|
|
|
90 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_POSTFIELDS, $params);
|
|
|
91 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_SSL_VERIFYPEER, false);
|
|
|
92 |
break;
|
|
|
93 |
|
|
|
94 |
case "GET":
|
|
|
95 |
if (strpos($url,'?') !== false) {
|
|
|
96 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_URL, $url."&".$params);
|
|
|
97 |
}
|
|
|
98 |
else{
|
|
|
99 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_URL, $url."?".$params);
|
|
|
100 |
}
|
|
|
101 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_SSL_VERIFYPEER, false);
|
|
|
102 |
break;
|
|
|
103 |
|
|
|
104 |
default:
|
|
|
105 |
log_message('error', 'Mcurl Class - Provided http method is not supported. Only POST and GET are currently supported.');
|
|
|
106 |
break;
|
|
|
107 |
}
|
|
|
108 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_RETURNTRANSFER, TRUE);
|
|
|
109 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_FOLLOWLOCATION, TRUE);
|
|
|
110 |
if(($key == 'tracking') || ($key == 'signup') || ($key == 'orderconfirmation_process')){
|
|
|
111 |
$cookiesStringToPass="";
|
|
|
112 |
foreach($_COOKIE as $cookie) {
|
|
|
113 |
if ($cookiesStringToPass) {
|
|
|
114 |
$cookiesStringToPass .= ';';
|
|
|
115 |
}
|
|
|
116 |
if(is_object(json_decode($cookie))){
|
|
|
117 |
$cookieInfo = json_decode($cookie);
|
|
|
118 |
foreach ($cookieInfo as $info) {
|
|
|
119 |
$cookiesStringToPass .= $info->name . '=' . addslashes($info->value);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
curl_setopt($this->calls[$key]["curl"], CURLOPT_COOKIE, $cookiesStringToPass);
|
|
|
124 |
}
|
|
|
125 |
curl_setopt_array($this->calls[$key]["curl"], $options);
|
|
|
126 |
curl_multi_add_handle($this->curl_parent,$this->calls[$key]["curl"]);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
// run the calls in the curl requests in $this->calls
|
|
|
130 |
function execute()
|
|
|
131 |
{
|
|
|
132 |
if (count($this->calls) <= 0)
|
|
|
133 |
return False;
|
|
|
134 |
if (count($this->calls))
|
|
|
135 |
{
|
|
|
136 |
// kick off the requests
|
|
|
137 |
do
|
|
|
138 |
{
|
|
|
139 |
//print_r($this->calls);
|
|
|
140 |
$multi_exec_handle = curl_multi_exec($this->curl_parent,$active);
|
|
|
141 |
} while ($active>0);
|
|
|
142 |
|
|
|
143 |
// after all requests finish, set errors and repsonses in $this->calls
|
|
|
144 |
foreach ($this->calls as $key => $call)
|
|
|
145 |
{
|
|
|
146 |
$error = curl_error($this->calls[$key]["curl"]);
|
|
|
147 |
if (!empty($error))
|
|
|
148 |
{
|
|
|
149 |
if($key != 'tracking' && $key != 'recommended_accessories'){
|
|
|
150 |
$this->calls[$key]["error"] = $error;
|
|
|
151 |
$url = 'http://www.saholic.com/'.uri_string();
|
|
|
152 |
$this->_ci->session->set_userdata('errorurl',$url);
|
|
|
153 |
//$errmsg ='<h3>OH SNAP!!!! :</h3><p>Something is not right. <br/><h1>But Relax</h1> <br/>We are working hard on it. </p>';
|
|
|
154 |
$msg = '
|
|
|
155 |
API for '.$key.' is not working.
|
|
|
156 |
|
|
|
157 |
url of API Failed ===> '.$this->calls[$key]['url'].'
|
|
|
158 |
parameters are ===> '.$call['params'].'
|
|
|
159 |
|
|
|
160 |
Access modifier is Public.
|
|
|
161 |
|
|
|
162 |
Error is ====> '.$this->calls[$key]["error"].'';
|
|
|
163 |
$this->write_log_send_mail('error',$msg);
|
|
|
164 |
//$this->session->set_flashdata('errormsg',$errmsg);
|
|
|
165 |
redirect(base_url().'error_page/index');
|
|
|
166 |
exit();
|
|
|
167 |
}
|
|
|
168 |
}
|
|
|
169 |
$this->calls[$key]["response"] = curl_multi_getcontent($this->calls[$key]["curl"]);
|
|
|
170 |
$http_status = curl_getinfo($this->calls[$key]["curl"], CURLINFO_HTTP_CODE);
|
|
|
171 |
if($key != 'tracking' && $key != 'recommended_accessories'){
|
|
|
172 |
if($http_status != 200) {
|
|
|
173 |
$url = 'http://www.saholic.com/'.uri_string();
|
|
|
174 |
$this->_ci->session->set_userdata('errorurl',$url);
|
|
|
175 |
$msg = '
|
|
|
176 |
API for '.$key.' is not working.
|
|
|
177 |
|
|
|
178 |
url of API Failed ===> '.$this->calls[$key]['url'].'
|
|
|
179 |
parameters are ===> '.$call['params'].'
|
|
|
180 |
|
|
|
181 |
Access modifier is Public.
|
|
|
182 |
|
|
|
183 |
Error is ====> '.$this->calls[$key]["error"].'';
|
|
|
184 |
$this->write_log_send_mail('error',$msg);
|
|
|
185 |
redirect(base_url().'error_page/index');
|
|
|
186 |
exit();
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
curl_multi_remove_handle($this->curl_parent,$this->calls[$key]["curl"]);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
curl_multi_close($this->curl_parent);
|
|
|
193 |
}
|
|
|
194 |
//print_r($this->calls);
|
|
|
195 |
return $this->calls;
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
function debug()
|
|
|
199 |
{
|
|
|
200 |
echo "<h2>mcurl debug</h2>";
|
|
|
201 |
foreach ($this->calls as $call)
|
|
|
202 |
{
|
|
|
203 |
echo '<p>url: <b>'.$call["url"].'</b></p>';
|
|
|
204 |
if (!is_null($call["error"]))
|
|
|
205 |
{
|
|
|
206 |
echo '<p style="color:red;">error: <b>'.$call["error"].'</b></p>';
|
|
|
207 |
}
|
|
|
208 |
echo '<textarea cols="100" rows="10">'.htmlentities($call["response"])."</textarea><hr>";
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
function write_log_send_mail($level = 'error', $msg, $php_error = FALSE){
|
|
|
213 |
$params = $this->_ci->config->item('email_params');
|
|
|
214 |
$this->_ci->load->library('email',$params);
|
|
|
215 |
$this->_ci->email->set_newline("\r\n");
|
|
|
216 |
$this->_ci->email->from('mobile@letsgomo.com', 'NO-REPLY');
|
|
|
217 |
$list = $this->_ci->config->item('email_list');
|
|
|
218 |
$this->_ci->email->to($list);
|
|
|
219 |
$this->_ci->email->cc('yashmeet@letsgomo.com');
|
|
|
220 |
$this->_ci->email->subject('The API failed on '.date('m/d/Y'));
|
|
|
221 |
$this->_ci->email->message('The API failed at '.date('m/d/Y h:i:s a', time()).' ------> '.$msg);
|
|
|
222 |
//send the email
|
|
|
223 |
$this->_ci->email->send();
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
/* End of file mcurl.php */
|
|
|
229 |
/* Location: ./application/libraries/mcurl.php */
|