comparison vendor/instaclick/php-webdriver/lib/WebDriver/Service/CurlService.php @ 14:1fec387a4317

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