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