|
1
|
|
|
<?php namespace Phabricator\Client\Curl; |
|
2
|
|
|
|
|
3
|
|
|
use Phabricator\Client\ClientInterface; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Simple CURL based client |
|
7
|
|
|
* |
|
8
|
|
|
* Phabricator PHP API |
|
9
|
|
|
* |
|
10
|
|
|
* @author Zoltán Borsos <[email protected]> |
|
11
|
|
|
* @package Phabricator |
|
12
|
|
|
* @subpackage Client\Curl |
|
13
|
|
|
* |
|
14
|
|
|
* @copyright Copyright 2016, Zoltán Borsos. |
|
15
|
|
|
* @license https://github.com/Zolli/Phabricator-PHP-API/blob/master/LICENSE.md |
|
16
|
|
|
* @link https://github.com/Zolli/Phabricator-PHP-API |
|
17
|
|
|
*/ |
|
18
|
|
|
class CurlClient implements ClientInterface { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Hold options that set in request when creating it |
|
22
|
|
|
* |
|
23
|
|
|
* @type array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $options = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritDoc} |
|
29
|
|
|
* |
|
30
|
|
|
* @codeCoverageIgnore |
|
31
|
|
|
*/ |
|
32
|
|
|
public function request($url, $requestData) { |
|
33
|
|
|
$request = new CurlRequest($url); |
|
34
|
|
|
$request->setPostData($requestData); |
|
35
|
|
|
|
|
36
|
|
|
$this->setOptionsOnRequest($request, $this->options); |
|
37
|
|
|
|
|
38
|
|
|
return $request->execute(); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Set option for CURL request |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $option CURLOPT_* constants |
|
45
|
|
|
* @param mixed $value The option value |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Phabricator\Client\Curl\CurlClient |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function setOption($option, $value) { |
|
50
|
1 |
|
$this->options[$option] = $value; |
|
51
|
|
|
|
|
52
|
1 |
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set CIRL request options from array. The array key is the option |
|
57
|
|
|
* and the value is used to option value. |
|
58
|
|
|
* |
|
59
|
|
|
* @param array $options |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Phabricator\Client\Curl\CurlClient |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function setOptionArray(array $options) { |
|
64
|
1 |
|
$this->options = $options; |
|
65
|
|
|
|
|
66
|
1 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the defined options on the given CurlRequest instance |
|
71
|
|
|
* |
|
72
|
|
|
* @param \Phabricator\Client\Curl\CurlRequest $request |
|
73
|
|
|
* @param $options |
|
74
|
|
|
* |
|
75
|
|
|
* @throws \BuildR\Foundation\Exception\RuntimeException |
|
76
|
|
|
* |
|
77
|
|
|
* @codeCoverageIgnore |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function setOptionsOnRequest(CurlRequest $request, $options) { |
|
80
|
|
|
foreach($options as $option => $value) { |
|
81
|
|
|
$request->setOption($options, $value); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|