| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace linkprofit\AmoCRM; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class RequestHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package linkprofit\AmoCRM | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class RequestHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @var mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var integer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $httpCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $httpErrors = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         301 => 'Moved permanently', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         400 => 'Bad request', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         401 => 'Unauthorized', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         403 => 'Forbidden', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         404 => 'Not found', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         500 => 'Internal server error', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         502 => 'Bad gateway', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         503 => 'Service unavailable' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected $successHttpCodes = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         200, 204 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected $subdomain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $link | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param array  $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string $contentType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $requestType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 3 |  |     public function performRequest($link, array $fields, $contentType = 'application/json', $requestType = 'POST') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |         $curl = curl_init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |         curl_setopt($curl, CURLOPT_USERAGENT, 'amoCRM-API-client/1.0'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |         curl_setopt($curl, CURLOPT_URL, $link); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $requestType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |         curl_setopt($curl, CURLOPT_POSTFIELDS, $this->getStringFields($fields, $contentType)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |         curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: '.$contentType]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |         curl_setopt($curl, CURLOPT_HEADER, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |         curl_setopt($curl, CURLOPT_COOKIEFILE, $this->getCookiePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |         curl_setopt($curl, CURLOPT_COOKIEJAR, $this->getCookiePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 3 |  |         $this->response = curl_exec($curl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |         $this->httpCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |         curl_close($curl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 4 |  |     public function getResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |         if (!$this->response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |         $this->encodeResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 3 |  |         return $this->response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param $subdomain string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 4 |  |     public function setSubdomain($subdomain) | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 4 |  |         $this->subdomain = $subdomain; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |     public function getSubdomain() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         return $this->subdomain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Encoding response from json, throw exception in case of wrong http code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 3 |  |     protected function encodeResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 3 |  |             if (!in_array($this->httpCode, $this->successHttpCodes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |                 throw new Exception(isset($this->httpErrors[$this->httpCode]) ? $this->httpErrors[$this->httpCode] : 'Undescribed error', $this->httpCode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  |             echo 'Error: ' . $e->getMessage() . PHP_EOL . 'Error code: ' . $e->getCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 3 |  |         $this->response = json_decode($this->response, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 3 |  |     protected function getCookiePath() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 3 |  |         return dirname(dirname(__FILE__)) . '/cookie.txt'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param array $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param       $contentType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 3 |  |     private function getStringFields(array $fields, $contentType) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 3 |  |         if ($contentType === 'application/json') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 3 |  |             return json_encode($fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return http_build_query($fields); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  | } |