1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\LinkedIn\Http; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use GuzzleHttp\Exception\TransferException; |
8
|
|
|
use Happyr\LinkedIn\Exceptions\LinkedInApiException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Tobias Nyholm |
12
|
|
|
*/ |
13
|
|
|
class GuzzleRequest implements RequestInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array lastHeaders |
17
|
|
|
*/ |
18
|
|
|
private $lastHeaders; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function send($method, $url, array $options = array()) |
24
|
|
|
{ |
25
|
|
|
// Do we use simple_xml for this request? |
26
|
|
|
$simpleXml = false; |
27
|
|
|
if (isset($options['simple_xml'])) { |
28
|
|
|
$simpleXml = (bool) $options['simple_xml']; |
29
|
|
|
unset($options['simple_xml']); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$client = $this->getClient(); |
33
|
|
|
$request = $client->createRequest($method, $url, $options); |
34
|
|
|
|
35
|
|
|
try { |
36
|
|
|
$response = $client->send($request); |
37
|
|
|
} catch (ClientException $guzzleException) { |
38
|
|
|
$e = new LinkedInApiException( |
|
|
|
|
39
|
|
|
array( |
40
|
|
|
'error_code' => $guzzleException->getCode(), |
41
|
|
|
'error' => array( |
42
|
|
|
'message' => $this->parseErrorMessage($guzzleException), |
43
|
|
|
'type' => 'GuzzleException', |
44
|
|
|
), |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
throw $e; |
49
|
|
|
} catch (TransferException $guzzleException) { |
50
|
|
|
$e = new LinkedInApiException( |
|
|
|
|
51
|
|
|
array( |
52
|
|
|
'error_code' => $guzzleException->getCode(), |
53
|
|
|
'error' => array( |
54
|
|
|
'message' => $guzzleException->getMessage(), |
55
|
|
|
'type' => 'GuzzleException', |
56
|
|
|
), |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
throw $e; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->lastHeaders = $response->getHeaders(); |
64
|
|
|
|
65
|
|
|
if ($this->isJsonResponse($response)) { |
66
|
|
|
return $response->json(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($simpleXml) { |
70
|
|
|
return $response->xml(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return (string) $response->getBody(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function getHeadersFromLastResponse() |
80
|
|
|
{ |
81
|
|
|
return $this->lastHeaders; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return Client |
86
|
|
|
*/ |
87
|
|
|
protected function getClient() |
88
|
|
|
{ |
89
|
|
|
return new Client(array( |
90
|
|
|
'User-Agent' => RequestInterface::USER_AGENT, |
91
|
|
|
)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Parse an exception and return its body's error message. |
96
|
|
|
* |
97
|
|
|
* @param ClientException $guzzleException |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function parseErrorMessage(ClientException $guzzleException) |
102
|
|
|
{ |
103
|
|
|
$response = $guzzleException->getResponse(); |
104
|
|
|
|
105
|
|
|
if ($this->isJsonResponse($response)) { |
106
|
|
|
$array = $response->json(); |
107
|
|
|
|
108
|
|
|
if (isset($array['message'])) { |
109
|
|
|
return $array['message']; |
110
|
|
|
}; |
111
|
|
|
|
112
|
|
|
if (isset($array['error_description'])) { |
113
|
|
|
return $array['error_description']; |
114
|
|
|
}; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
return (string)$response->xml()->message; |
119
|
|
|
} catch (\Exception $ex) { |
120
|
|
|
return 'Excpetion while processing error response. Error reason phrase: ' . $response->getReasonPhrase(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $guzzleResponse |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
protected function isJsonResponse($guzzleResponse) |
130
|
|
|
{ |
131
|
|
|
return false !== strstr($guzzleResponse->getHeader('Content-Type'), 'application/json'); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.