| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace WrapIt\Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use WrapIt\Exceptions\WrapItParameterException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use WrapIt\Exceptions\WrapItHTTPException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class Requester | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package WrapIt | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | abstract class Requester { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     protected $domain = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 24 |  |     protected function request($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 24 |  |         $data = array_merge(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 24 |  |             "url" => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 12 |  |             "get" => array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 12 |  |             "post" => array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 12 |  |             "body_type" => "application/x-www-form-urlencoded", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 12 |  |             "body_charset" => "UTF-8", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 12 |  |             "headers" => array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 12 |  |             "response_type" => "json", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 12 |  |             "useragent" => "WrapIt-HTTP/1.0", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 12 |  |             "method" => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             "compression" => "none" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 24 |  |         ), $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 24 |  |         if ($data["url"] === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             throw new WrapItParameterException("Missing data: url"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 24 |  |         $url = $data["url"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 24 |  |         if ($data["get"] !== null && count($data["get"]) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 6 |  |             $url .= "?" . http_build_query($data["get"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 24 |  |         $ch = curl_init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 24 |  |         curl_setopt($ch, CURLOPT_URL, $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 24 |  |         curl_setopt($ch, CURLOPT_USERAGENT, $data["useragent"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 24 |  |         if ($data["post"] != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 12 |  |             $data["headers"][] = "Content-type: " . $data["body_type"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 12 |  |             $data["headers"][] = "Charset: " . $data["body_charset"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 12 |  |             switch ($data["body_type"]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 10 |  |                 case "application/x-www-form-urlencoded": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 12 |  |                     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data["post"])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 12 |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 case "application/json": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data["post"])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 case "text/plain": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     curl_setopt($ch, CURLOPT_POSTFIELDS, $data["post"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 case "text/xml": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     if (is_string($data["post"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                         curl_setopt($ch, CURLOPT_POSTFIELDS, $data["post"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                         throw new WrapItParameterException("XML data must be pre-processed!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     throw new WrapItParameterException("Invalid body type"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 6 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 12 |  |             if ($data["method"] !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $data["method"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 12 |  |                 curl_setopt($ch, CURLOPT_POST, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 6 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 12 |  |             if ($data["method"] !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $data["method"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 24 |  |         if ($data["headers"] !== null && count($data["headers"]) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 24 |  |             curl_setopt($ch, CURLOPT_HTTPHEADER, $data["headers"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 24 |  |         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 24 |  |         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 24 |  |         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 24 |  |         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 24 |  |         switch ($data["compression"]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 20 |  |             case "gzip": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 curl_setopt($ch, CURLOPT_ENCODING , "gzip"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 12 |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 24 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 24 |  |         if ($data["response_type"] == "head") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             curl_setopt($ch, CURLOPT_HEADER, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 24 |  |         $result = curl_exec($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 24 |  |         $response_header = curl_getinfo($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 24 |  |         curl_close($ch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 24 |  |         if ($response_header["http_code"] == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             throw new WrapItHTTPException("HTTP Connection error"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 24 |  |         switch ($data["response_type"]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 20 |  |             case "json": | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 24 |  |                 $json = json_decode($result, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 24 |  |                 if ($json !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 24 |  |                     return $json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                     return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             case "head": | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 return  $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 6 |  |     public function getDomain() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 6 |  |         return $this->domain; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |     abstract public function get($api, $data); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |     abstract public function post($api, $data); | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  |