response() public method

public response ( )
Example #1
0
 function request($url, $data = null, $timeout = 10, $headers = array())
 {
     $ch = curl_init();
     $options = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => 'utf-8', CURLOPT_USERAGENT => self::agent(), CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => $timeout, CURLOPT_TIMEOUT => $timeout, CURLOPT_MAXREDIRS => 10, CURLOPT_SSL_VERIFYPEER => false);
     if (!empty($headers)) {
         $options[CURLOPT_HTTPHEADER] = $headers;
     }
     if (!empty($data)) {
         $options[CURLOPT_POST] = true;
         $options[CURLOPT_POSTFIELDS] = $data;
     }
     curl_setopt_array($ch, $options);
     $content = curl_exec($ch);
     $error = curl_errno($ch);
     $message = curl_error($ch);
     $response = curl_getinfo($ch);
     curl_close($ch);
     $response['error'] = $error;
     $response['message'] = $message;
     $response['content'] = $content;
     self::$response = $response;
     if (a::get($response, 'error')) {
         return array('status' => 'error', 'msg' => 'The remote request failed: ' . $response['message']);
     }
     if (a::get($response, 'http_code') >= 400) {
         return array('status' => 'error', 'msg' => 'The remote request failed - code: ' . $response['http_code'], 'code' => $response['http_code']);
     }
     return $response;
 }