Subversion Repositories SmartDukaan

Rev

Rev 11805 | Rev 13061 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
	{
11387 lgm 26
 
10582 lgm 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
		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
		curl_setopt($this->calls[$key]["curl"], CURLOPT_RETURNTRANSFER, TRUE);
108
		curl_setopt($this->calls[$key]["curl"], CURLOPT_FOLLOWLOCATION, TRUE);
11109 lgm 109
		if(($key == 'tracking') || ($key == 'signup') || ($key == 'orderconfirmation_process')){
110
			$cookiesStringToPass="";
111
			foreach($_COOKIE as $cookie) {
112
				if ($cookiesStringToPass) {
113
	     			$cookiesStringToPass  .= ';';
114
	    		}
115
	    		if(is_object(json_decode($cookie))){
116
	    			$cookieInfo = json_decode($cookie);
117
	    			foreach ($cookieInfo as $info) {
118
	    				$cookiesStringToPass .= $info->name . '=' . addslashes($info->value);
119
	    			}
120
	    		}
121
			}
122
			curl_setopt($this->calls[$key]["curl"], CURLOPT_COOKIE, $cookiesStringToPass);
12999 anikendra 123
			foreach (getallheaders() as $name => $value) {
124
			    if($name == "X-Forwarded-For"){
125
			    	$headers = array('X-Forwarded-For : '.$value);
126
			    	curl_setopt($this->calls[$key]["curl"], CURLOPT_HTTPHEADER, $headers);
127
			    }
128
			}
11109 lgm 129
		}
10582 lgm 130
		curl_setopt_array($this->calls[$key]["curl"], $options);
131
		curl_multi_add_handle($this->curl_parent,$this->calls[$key]["curl"]);		 
132
	}
133
 
134
	// run the calls in the curl requests in $this->calls
135
	function execute()
136
	{
11170 lgm 137
		if (count($this->calls) <= 0)
138
			return False;
10582 lgm 139
		if (count($this->calls))
140
		{
141
			// kick off the requests
142
			do
143
			{
11170 lgm 144
				//print_r($this->calls);
10582 lgm 145
				$multi_exec_handle = curl_multi_exec($this->curl_parent,$active);
146
			} while ($active>0);
147
 
148
			// after all requests finish, set errors and repsonses in $this->calls
149
			foreach ($this->calls as $key => $call)
150
			{
151
				$error = curl_error($this->calls[$key]["curl"]);
152
				if (!empty($error))
153
				{
11387 lgm 154
					if($key != 'tracking' && $key != 'recommended_accessories'){
10582 lgm 155
					$this->calls[$key]["error"] = $error;
11510 lgm 156
					$url = 'http://www.saholic.com/'.uri_string();
11387 lgm 157
					$this->_ci->session->set_userdata('errorurl',$url);
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
					$msg = '
160
					API for '.$key.' is not working.
161
 
162
					url of API Failed ===> '.$this->calls[$key]['url'].'
11681 lgm 163
					parameters are ===> '.$call['params'].'
164
 
11387 lgm 165
					Access modifier is Public.
166
 
167
					Error is ====> '.$this->calls[$key]["error"].'';
168
					$this->write_log_send_mail('error',$msg);
10739 lgm 169
					//$this->session->set_flashdata('errormsg',$errmsg);
11387 lgm 170
					redirect(base_url().'error_page/index');
171
					exit();
10739 lgm 172
					}
10582 lgm 173
				}
174
				$this->calls[$key]["response"] = curl_multi_getcontent($this->calls[$key]["curl"]);
10800 lgm 175
				$http_status = curl_getinfo($this->calls[$key]["curl"], CURLINFO_HTTP_CODE);
11226 lgm 176
				if($key != 'tracking' && $key != 'recommended_accessories'){
10802 lgm 177
					if($http_status != 200) {
11354 lgm 178
						$url = 'http://www.saholic.com/'.uri_string();
179
						$this->_ci->session->set_userdata('errorurl',$url);
11387 lgm 180
						$msg = '
181
						API for '.$key.' is not working.
182
 
183
						url of API Failed ===> '.$this->calls[$key]['url'].'
11681 lgm 184
						parameters are ===> '.$call['params'].'
11387 lgm 185
 
186
						Access modifier is Public.
187
 
188
						Error is ====> '.$this->calls[$key]["error"].'';
189
						$this->write_log_send_mail('error',$msg);
10802 lgm 190
        				redirect(base_url().'error_page/index');
191
        				exit();
192
					}
10800 lgm 193
				}
10756 lgm 194
				curl_multi_remove_handle($this->curl_parent,$this->calls[$key]["curl"]);
10582 lgm 195
			}
196
 
197
			curl_multi_close($this->curl_parent);
198
		}
11103 lgm 199
		//print_r($this->calls);
10582 lgm 200
		return $this->calls;
201
	}
202
 
203
	function debug()
204
	{
205
		echo "<h2>mcurl debug</h2>";
206
		foreach ($this->calls as $call)
207
		{
208
			echo '<p>url: <b>'.$call["url"].'</b></p>';
209
			if (!is_null($call["error"]))
210
			{
211
				echo '<p style="color:red;">error: <b>'.$call["error"].'</b></p>';
212
			}
213
			echo '<textarea cols="100" rows="10">'.htmlentities($call["response"])."</textarea><hr>";
214
		}
215
	}
216
 
11387 lgm 217
	function write_log_send_mail($level = 'error', $msg, $php_error = FALSE){ 
218
	 	$params = $this->_ci->config->item('email_params'); 
219
		$this->_ci->load->library('email',$params);
220
		$this->_ci->email->set_newline("\r\n");
221
	 	$this->_ci->email->from('mobile@letsgomo.com', 'NO-REPLY');
222
	 	$list = $this->_ci->config->item('email_list');
223
		$this->_ci->email->to($list);
224
		$this->_ci->email->cc('yashmeet@letsgomo.com');		
225
		$this->_ci->email->subject('The API failed on '.date('m/d/Y'));		
226
		$this->_ci->email->message('The API failed at '.date('m/d/Y h:i:s a', time()).' ------> '.$msg);
227
		//send the email 	
228
		$this->_ci->email->send();
229
	}
230
 
10582 lgm 231
}
232
 
233
/* End of file mcurl.php */
12999 anikendra 234
/* Location: ./application/libraries/mcurl.php */