annotate vendor/instaclick/php-webdriver/lib/WebDriver/Service/CurlService.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Copyright 2004-2017 Facebook. All Rights Reserved.
Chris@0 4 *
Chris@0 5 * Licensed under the Apache License, Version 2.0 (the "License");
Chris@0 6 * you may not use this file except in compliance with the License.
Chris@0 7 * You may obtain a copy of the License at
Chris@0 8 *
Chris@0 9 * http://www.apache.org/licenses/LICENSE-2.0
Chris@0 10 *
Chris@0 11 * Unless required by applicable law or agreed to in writing, software
Chris@0 12 * distributed under the License is distributed on an "AS IS" BASIS,
Chris@0 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Chris@0 14 * See the License for the specific language governing permissions and
Chris@0 15 * limitations under the License.
Chris@0 16 *
Chris@0 17 * @package WebDriver
Chris@0 18 *
Chris@0 19 * @author Justin Bishop <jubishop@gmail.com>
Chris@0 20 * @author Anthon Pang <apang@softwaredevelopment.ca>
Chris@0 21 * @author Fabrizio Branca <mail@fabrizio-branca.de>
Chris@0 22 */
Chris@0 23
Chris@0 24 namespace WebDriver\Service;
Chris@0 25
Chris@0 26 use WebDriver\Exception as WebDriverException;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * WebDriver\Service\CurlService class
Chris@0 30 *
Chris@0 31 * @package WebDriver
Chris@0 32 */
Chris@0 33 class CurlService implements CurlServiceInterface
Chris@0 34 {
Chris@0 35 /**
Chris@0 36 * {@inheritdoc}
Chris@0 37 */
Chris@0 38 public function execute($requestMethod, $url, $parameters = null, $extraOptions = array())
Chris@0 39 {
Chris@0 40 $customHeaders = array(
Chris@0 41 'Content-Type: application/json;charset=UTF-8',
Chris@0 42 'Accept: application/json;charset=UTF-8',
Chris@0 43 );
Chris@0 44
Chris@0 45 $curl = curl_init($url);
Chris@0 46 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Chris@0 47
Chris@0 48 switch ($requestMethod) {
Chris@0 49 case 'GET':
Chris@0 50 break;
Chris@0 51
Chris@0 52 case 'POST':
Chris@0 53 if ($parameters && is_array($parameters)) {
Chris@0 54 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
Chris@0 55 } else {
Chris@0 56 $customHeaders[] = 'Content-Length: 0';
Chris@0 57 }
Chris@0 58
Chris@0 59 // Suppress "Expect: 100-continue" header automatically added by cURL that
Chris@0 60 // causes a 1 second delay if the remote server does not support Expect.
Chris@0 61 $customHeaders[] = 'Expect:';
Chris@0 62
Chris@0 63 curl_setopt($curl, CURLOPT_POST, true);
Chris@0 64 break;
Chris@0 65
Chris@0 66 case 'DELETE':
Chris@0 67 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
Chris@0 68 break;
Chris@0 69
Chris@0 70 case 'PUT':
Chris@0 71 if ($parameters && is_array($parameters)) {
Chris@0 72 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($parameters));
Chris@0 73 } else {
Chris@0 74 $customHeaders[] = 'Content-Length: 0';
Chris@0 75 }
Chris@0 76
Chris@0 77 // Suppress "Expect: 100-continue" header automatically added by cURL that
Chris@0 78 // causes a 1 second delay if the remote server does not support Expect.
Chris@0 79 $customHeaders[] = 'Expect:';
Chris@0 80
Chris@0 81 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
Chris@0 82 break;
Chris@0 83 }
Chris@0 84
Chris@0 85 foreach ($extraOptions as $option => $value) {
Chris@0 86 curl_setopt($curl, $option, $value);
Chris@0 87 }
Chris@0 88
Chris@0 89 curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders);
Chris@0 90
Chris@0 91 $rawResult = trim(curl_exec($curl));
Chris@0 92
Chris@0 93 $info = curl_getinfo($curl);
Chris@0 94 $info['request_method'] = $requestMethod;
Chris@0 95
Chris@0 96 if (array_key_exists(CURLOPT_FAILONERROR, $extraOptions) &&
Chris@0 97 $extraOptions[CURLOPT_FAILONERROR] &&
Chris@0 98 CURLE_GOT_NOTHING !== ($errno = curl_errno($curl)) &&
Chris@0 99 $error = curl_error($curl)
Chris@0 100 ) {
Chris@0 101 curl_close($curl);
Chris@0 102
Chris@0 103 throw WebDriverException::factory(
Chris@0 104 WebDriverException::CURL_EXEC,
Chris@0 105 sprintf(
Chris@0 106 "Curl error thrown for http %s to %s%s\n\n%s",
Chris@0 107 $requestMethod,
Chris@0 108 $url,
Chris@0 109 $parameters && is_array($parameters) ? ' with params: ' . json_encode($parameters) : '',
Chris@0 110 $error
Chris@0 111 ),
Chris@0 112 $errno,
Chris@0 113 null,
Chris@0 114 $info
Chris@0 115 );
Chris@0 116 }
Chris@0 117
Chris@0 118 curl_close($curl);
Chris@0 119
Chris@0 120 return array($rawResult, $info);
Chris@0 121 }
Chris@0 122 }