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