forked from cloudinary/cloudinary_php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpClient.php
More file actions
106 lines (91 loc) · 2.66 KB
/
Copy pathHttpClient.php
File metadata and controls
106 lines (91 loc) · 2.66 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
<?php
namespace Cloudinary;
/**
* Class HttpClient
* @package Cloudinary
*/
class HttpClient
{
const DEFAULT_HTTP_TIMEOUT = 60;
/**
* @var int HTTP timeout in seconds
*/
private $timeout;
/**
* HttpClient constructor.
*
* @param $options
*/
public function __construct($options = null)
{
$this->timeout = \Cloudinary::option_get($options, "timeout", self::DEFAULT_HTTP_TIMEOUT);
}
/**
* Get JSON as associative array from specified URL
*
* @param string $url URL of the JSON
*
* @return array Associative array that represents JSON object
*
* @throws Error
*/
public function getJSON($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, \Cloudinary::userAgent());
$response = $this->execute($ch);
$curl_error = null;
if (curl_errno($ch)) {
$curl_error = curl_error($ch);
}
curl_close($ch);
if ($curl_error != null) {
throw new Error("Error in sending request to server - " . $curl_error);
}
if ($response->responseCode != 200) {
throw new Error("Server returned unexpected status code - {$response->responseCode} - {$response->body}");
}
return self::parseJSONResponse($response);
}
/**
* Executes HTTP request, parses response headers, leaves body as a string
*
* Based on http://snipplr.com/view/17242/
*
* @param resource $ch cURL handle
*
* @return \stdClass Containing headers, body, responseCode properties
*/
protected static function execute($ch)
{
$content = curl_exec($ch);
$result = new \stdClass;
$result->body = trim($content);
$result->responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return $result;
}
/**
* Parses JSON string from response body.
*
* @param \stdClass $response Class representing response
*
* @return mixed Decoded JSON object
*
* @throws Error
*/
protected static function parseJSONResponse($response)
{
$result = json_decode($response->body, true);
if ($result == null) {
$error = json_last_error();
throw new Error(
"Error parsing server response ({$response->responseCode}) - {$response->body}. Got - {$error}"
);
}
return $result;
}
}