예제 #1
0
 /**
  * Makes an HTTP request using CURL.
  *
  * @param $url string the URL to request
  * @param string $method The request method ('GET', 'POST', 'PUT', 'DELETE', etc)
  * @param null $data Any additional data to include in the payload.
  * @param null $headers Additional headers to send.
  * @param int $timeout The request timeout in milliseconds
  * @return bool|string The response string, or false if there was an error.
  */
 public static function request_curl($url, $method = 'GET', $data = null, $headers = null, $timeout = 300)
 {
     $cn = curl_init();
     if (!empty($data)) {
         if (strtolower($method) == 'post' || strtolower($method) == 'put') {
             curl_setopt($cn, CURLOPT_POSTFIELDS, http_build_query($data));
         } else {
             $url = HttpHelper::appendToUrl($url, $data);
         }
     }
     curl_setopt($cn, CURLOPT_URL, $url);
     curl_setopt($cn, CURLOPT_TIMEOUT, $timeout);
     curl_setopt($cn, CURLOPT_CUSTOMREQUEST, $method);
     curl_setopt($cn, CURLOPT_FORBID_REUSE, 0);
     curl_setopt($cn, CURLOPT_ENCODING, 'gzip,deflate');
     if (!self::$verifySsl) {
         curl_setopt($cn, CURLOPT_SSL_VERIFYHOST, 0);
         curl_setopt($cn, CURLOPT_SSL_VERIFYPEER, 0);
     }
     if (!empty($headers)) {
         $h = array();
         while (list($header, $headerValue) = each($headers)) {
             array_push($h, $header . ': ' . $headerValue);
         }
         curl_setopt($cn, CURLOPT_HTTPHEADER, $h);
     }
     try {
         ob_start();
         if (curl_exec($cn)) {
             return ob_get_clean();
         } else {
             ob_end_clean();
             return false;
         }
     } catch (\Exception $e) {
         ob_end_clean();
         throw $e;
     }
 }