Esempio n. 1
0
 public static function Curl($http_type, $full_url, $payload = null, $escape_payload = true, $cookies = array())
 {
     if (!self::$curl) {
         self::$curl = curl_init();
     }
     curl_setopt(self::$curl, CURLOPT_URL, $full_url);
     curl_setopt(self::$curl, CURLOPT_CUSTOMREQUEST, $http_type);
     curl_setopt(self::$curl, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt(self::$curl, CURLINFO_HEADER_OUT, TRUE);
     curl_setopt(self::$curl, CURLOPT_HEADER, TRUE);
     curl_setopt(self::$curl, CURLOPT_CONNECTTIMEOUT, WebDriver::$CurlConnectTimeoutSec);
     curl_setopt(self::$curl, CURLOPT_TIMEOUT, WebDriver::$CurlTimeoutSec);
     if (($http_type === "POST" || $http_type === "PUT") && $payload !== null) {
         if ($escape_payload && (is_array($payload) || is_object($payload))) {
             $payload = http_build_query($payload);
         }
         curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $payload);
     }
     $headers = array('Expect:', 'Accept: application/json');
     if ($payload !== null && is_string($payload) && json_decode($payload) !== null) {
         $headers[] = 'Content-Type: application/json; charset=utf-8';
     }
     if (is_string($payload)) {
         $headers[] = 'Content-Length: ' . strlen($payload);
     }
     curl_setopt(self::$curl, CURLOPT_HTTPHEADER, $headers);
     if (!empty($cookies)) {
         $cookie_string = http_build_query($cookies, '', '; ');
         curl_setopt(self::$curl, CURLOPT_COOKIE, $cookie_string);
     }
     $full_response = curl_exec(self::$curl);
     $request_header = curl_getinfo(self::$curl, CURLINFO_HEADER_OUT);
     WebDriver::LogDebug($request_header);
     WebDriver::LogDebug($payload);
     WebDriver::LogDebug("-");
     WebDriver::LogDebug($full_response);
     WebDriver::LogDebug("=====");
     $error = curl_error(self::$curl);
     PHPUnit_Framework_Assert::assertEquals("", $error, "Curl error: {$error}\nMethod: {$http_type}\nURL: {$full_url}\n" . print_r($payload, true));
     $response_parts = explode("\r\n\r\n", $full_response, 2);
     $response['header'] = $response_parts[0];
     if (!empty($response_parts[1])) {
         $response['body'] = $response_parts[1];
     }
     return $response;
 }