1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2016 François Kooman <[email protected]>. |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
8
|
|
|
* License, or (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU Affero General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace fkooman\OAuth\Client; |
20
|
|
|
|
21
|
|
|
use RuntimeException; |
22
|
|
|
|
23
|
|
|
class CurlHttpClient implements HttpClientInterface |
24
|
|
|
{ |
25
|
|
|
/** @var resource */ |
26
|
|
|
private $curlChannel; |
27
|
|
|
|
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
if (false === $this->curlChannel = curl_init()) { |
31
|
|
|
throw new RuntimeException('unable to create cURL channel'); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __destruct() |
36
|
|
|
{ |
37
|
|
|
curl_close($this->curlChannel); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function post(Provider $provider, array $postData) |
41
|
|
|
{ |
42
|
|
|
$curlOptions = [ |
43
|
|
|
CURLOPT_URL => $provider->getTokenEndpoint(), |
44
|
|
|
CURLOPT_POSTFIELDS => http_build_query($postData), |
45
|
|
|
CURLOPT_USERPWD => sprintf('%s:%s', $provider->getId(), $provider->getSecret()), |
46
|
|
|
CURLOPT_HEADER => 0, |
47
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
48
|
|
|
CURLOPT_FOLLOWLOCATION => 0, |
49
|
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
if (false === curl_setopt_array($this->curlChannel, $curlOptions)) { |
53
|
|
|
throw new RuntimeException('unable to set cURL options'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (false === $responseData = curl_exec($this->curlChannel)) { |
57
|
|
|
$curlError = curl_error($this->curlChannel); |
58
|
|
|
throw new RuntimeException(sprintf('failure performing the HTTP request: "%s"', $curlError)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE), |
|
|
|
|
62
|
|
|
return json_decode($responseData, true); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.