Chris@14: Chris@14: * @author Anthon Pang Chris@14: * @author Fabrizio Branca Chris@14: */ Chris@14: Chris@14: namespace WebDriver\Service; Chris@14: Chris@14: use WebDriver\Exception as WebDriverException; Chris@14: Chris@14: /** Chris@14: * WebDriver\Service\CurlService class Chris@14: * Chris@14: * @package WebDriver Chris@14: */ Chris@14: class CurlService implements CurlServiceInterface Chris@14: { Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function execute($requestMethod, $url, $parameters = null, $extraOptions = array()) Chris@14: { Chris@14: $customHeaders = array( Chris@14: 'Content-Type: application/json;charset=UTF-8', Chris@14: 'Accept: application/json;charset=UTF-8', Chris@14: ); Chris@14: Chris@14: $curl = curl_init($url); Chris@14: curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); Chris@14: Chris@14: switch ($requestMethod) { Chris@14: case 'GET': Chris@14: break; Chris@14: Chris@14: case 'POST': Chris@14: if ($parameters && is_array($parameters)) { Chris@14: curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); Chris@14: } else { Chris@14: $customHeaders[] = 'Content-Length: 0'; Chris@14: } Chris@14: Chris@14: // Suppress "Expect: 100-continue" header automatically added by cURL that Chris@14: // causes a 1 second delay if the remote server does not support Expect. Chris@14: $customHeaders[] = 'Expect:'; Chris@14: Chris@14: curl_setopt($curl, CURLOPT_POST, true); Chris@14: break; Chris@14: Chris@14: case 'DELETE': Chris@14: curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); Chris@14: break; Chris@14: Chris@14: case 'PUT': Chris@14: if ($parameters && is_array($parameters)) { Chris@14: curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters)); Chris@14: } else { Chris@14: $customHeaders[] = 'Content-Length: 0'; Chris@14: } Chris@14: Chris@14: // Suppress "Expect: 100-continue" header automatically added by cURL that Chris@14: // causes a 1 second delay if the remote server does not support Expect. Chris@14: $customHeaders[] = 'Expect:'; Chris@14: Chris@14: curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); Chris@14: break; Chris@14: } Chris@14: Chris@14: foreach ($extraOptions as $option => $value) { Chris@14: curl_setopt($curl, $option, $value); Chris@14: } Chris@14: Chris@14: curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders); Chris@14: Chris@14: $rawResult = trim(curl_exec($curl)); Chris@14: Chris@14: $info = curl_getinfo($curl); Chris@14: $info['request_method'] = $requestMethod; Chris@14: Chris@14: if (array_key_exists(CURLOPT_FAILONERROR, $extraOptions) && Chris@14: $extraOptions[CURLOPT_FAILONERROR] && Chris@14: CURLE_GOT_NOTHING !== ($errno = curl_errno($curl)) && Chris@14: $error = curl_error($curl) Chris@14: ) { Chris@14: curl_close($curl); Chris@14: Chris@14: throw WebDriverException::factory( Chris@14: WebDriverException::CURL_EXEC, Chris@14: sprintf( Chris@14: "Curl error thrown for http %s to %s%s\n\n%s", Chris@14: $requestMethod, Chris@14: $url, Chris@14: $parameters && is_array($parameters) ? ' with params: ' . json_encode($parameters) : '', Chris@14: $error Chris@14: ), Chris@14: $errno, Chris@14: null, Chris@14: $info Chris@14: ); Chris@14: } Chris@14: Chris@14: curl_close($curl); Chris@14: Chris@14: return array($rawResult, $info); Chris@14: } Chris@14: }