|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Pinterest PHP library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Hans Ott <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE.md. |
|
10
|
|
|
* |
|
11
|
|
|
* Source: https://github.com/hansott/pinterest-php |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Pinterest\Http; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use Buzz\Browser; |
|
18
|
|
|
use Pinterest\Image; |
|
19
|
|
|
use Buzz\Client\Curl; |
|
20
|
|
|
use Buzz\Message\Form\FormUpload; |
|
21
|
|
|
use Buzz\Message\Form\FormRequest; |
|
22
|
|
|
use Buzz\Exception\RequestException; |
|
23
|
|
|
use Buzz\Message\Response as BuzzResponse; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The implemented http client class (uses Buzz). |
|
27
|
|
|
* |
|
28
|
|
|
* @link https://github.com/kriswallsmith/Buzz |
|
29
|
|
|
* |
|
30
|
|
|
* @author Toon Daelman <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class BuzzClient implements ClientInterface |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Buzz browser. |
|
36
|
|
|
* |
|
37
|
|
|
* @var Buzz\Browser |
|
38
|
|
|
*/ |
|
39
|
|
|
private $client; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates a new buzz client. |
|
43
|
|
|
*/ |
|
44
|
60 |
|
public function __construct() |
|
45
|
|
|
{ |
|
46
|
60 |
|
$curl = new Curl(); |
|
47
|
60 |
|
$this->client = new Browser($curl); |
|
|
|
|
|
|
48
|
60 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Converts a buzz response to a pinterest response. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Request $request The request. |
|
54
|
|
|
* @param BuzzResponse $buzzResponse The buzz response. |
|
55
|
|
|
* |
|
56
|
|
|
* @return Response The response. |
|
57
|
|
|
*/ |
|
58
|
|
|
private static function convertResponse(Request $request, BuzzResponse $buzzResponse) |
|
59
|
|
|
{ |
|
60
|
|
|
$statusCode = $buzzResponse->getStatusCode(); |
|
61
|
|
|
$rawBody = (string) $buzzResponse->getContent(); |
|
62
|
|
|
|
|
63
|
|
|
$rawHeaders = $buzzResponse->getHeaders(); |
|
64
|
|
|
$headers = array(); |
|
65
|
|
|
foreach ($rawHeaders as $header) { |
|
66
|
|
|
if (stristr($header, 'HTTP/1.')) { |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$parts = explode(': ', $header); |
|
71
|
|
|
|
|
72
|
|
|
if (count($parts) !== 2) { |
|
73
|
|
|
$headers[$parts[0]] = ''; |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
list ($key, $value) = $parts; |
|
78
|
|
|
$headers[$key] = $value; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return new Response($request, $statusCode, $rawBody, $headers); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Executes a http request. |
|
86
|
|
|
* |
|
87
|
|
|
* @param Request $request The http request. |
|
88
|
|
|
* |
|
89
|
|
|
* @return Response The http response. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function execute(Request $request) |
|
92
|
|
|
{ |
|
93
|
|
|
$method = $request->getMethod(); |
|
94
|
|
|
$endpoint = $request->getEndpoint(); |
|
95
|
|
|
$params = $request->getParams(); |
|
96
|
|
|
$headers = $request->getHeaders(); |
|
97
|
|
|
|
|
98
|
|
|
try { |
|
99
|
|
|
if ($method === 'GET') { |
|
100
|
|
|
$buzzResponse = $this->client->call( |
|
101
|
|
|
$endpoint.'?'.http_build_query($params), |
|
102
|
|
|
$method, |
|
103
|
|
|
$headers, |
|
104
|
|
|
array() |
|
105
|
|
|
); |
|
106
|
|
|
} else { |
|
107
|
|
|
$buzzRequest = new FormRequest(); |
|
108
|
|
|
$buzzRequest->fromUrl($endpoint); |
|
109
|
|
|
$buzzRequest->setMethod($method); |
|
110
|
|
|
$buzzRequest->setHeaders($headers); |
|
111
|
|
|
foreach ($params as $key => $value) { |
|
112
|
|
|
if ($value instanceof Image) { |
|
113
|
|
|
$value = new FormUpload($value->getData()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$buzzRequest->setField($key, $value); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$buzzResponse = new BuzzResponse(); |
|
120
|
|
|
$this->client->send($buzzRequest, $buzzResponse); |
|
121
|
|
|
} |
|
122
|
|
|
} catch (RequestException $e) { |
|
123
|
|
|
throw new Exception($e->getMessage()); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return static::convertResponse($request, $buzzResponse); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..