Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0c86ed

Browse files
Merge pull request #72 from younes0/master
Guzzle Resolver added
2 parents d53edd0 + d6a4e54 commit f0c86ed

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

‎README.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@ The resolver configuration is defined under the "resolver" key and it has two op
147147
* config: The options passed to the class. If you use the default curl class, the config are the same than the [curl_setopt PHP function](http://php.net/manual/en/function.curl-setopt.php)
148148

149149
```php
150+
151+
// CURL
150152
$config = [
151153
'resolver' => [
152-
'Embed\\RequestResolvers\\Curl' //The default resolver used
154+
'class' => 'Embed\\RequestResolvers\\Curl' //The default resolver used
153155

154156
'config' => [
155157
CURLOPT_MAXREDIRS => 20,
@@ -164,6 +166,18 @@ $config = [
164166
]
165167
]
166168
];
169+
170+
// Guzzle (5.x)
171+
$config = [
172+
'resolver' => [
173+
'class' => 'Embed\\RequestResolvers\\Guzzle5', // Guzzle5 resolver used
174+
175+
'config' => [
176+
// optional: if you need to use your custom Guzzle instance
177+
'client' => $myGuzzleClient,
178+
]
179+
]
180+
];
167181
```
168182

169183
### Image info

‎src/RequestResolvers/Guzzle5.php‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Embed\RequestResolvers;
4+
5+
use GuzzleHttp\Client;
6+
7+
class Guzzle5 implements RequestResolverInterface
8+
{
9+
/**
10+
* @var Client
11+
*/
12+
protected $client;
13+
14+
/**
15+
* @var GuzzleHttp\Request
16+
*/
17+
protected $request;
18+
19+
/**
20+
* @var GuzzleHttp\Response
21+
*/
22+
protected $response;
23+
24+
protected $defaultConfig = [
25+
'verify' => false,
26+
'timeout' => 10,
27+
'connect_timeout' => 20,
28+
'headers' => [
29+
'User-Agent' => 'Embed PHP Library',
30+
],
31+
'allow_redirects' => [
32+
'max' => 20,
33+
'referer' => true,
34+
],
35+
];
36+
37+
/**
38+
* Constructor. Sets the url
39+
*
40+
* @param string $url The url value
41+
* @param array $config The resolver configuration
42+
*/
43+
public function __construct($url, array $config)
44+
{
45+
$this->client = isset($config['client']) ? $config['client'] : new Client([
46+
'defaults' => $this->defaultConfig,
47+
]);
48+
49+
$this->request = $this->client->createRequest('GET', $url);
50+
}
51+
52+
/**
53+
* Get the http code of the url, for example: 200
54+
*
55+
* @return int The http code
56+
*/
57+
public function getHttpCode()
58+
{
59+
return $this->getResponse()->getStatusCode();
60+
}
61+
62+
/**
63+
* Get the content-type of the url, for example: text/html
64+
*
65+
* @return string The content-type header or null
66+
*/
67+
public function getMimeType()
68+
{
69+
return $this->getResponse()->getHeader('Content-Type');
70+
}
71+
72+
/**
73+
* Get the content of the url
74+
*
75+
* @return string The content or false
76+
*/
77+
public function getContent()
78+
{
79+
return $this->getResponse()->getBody()->getContents();
80+
}
81+
82+
/**
83+
* Return the final url (after all possible redirects)
84+
*
85+
* @return string The final url
86+
*/
87+
public function getUrl()
88+
{
89+
return $this->getResponse()->getEffectiveUrl();
90+
}
91+
92+
/**
93+
* Return the http request info (for debug purposes)
94+
*
95+
* @return array
96+
*/
97+
public function getRequestInfo()
98+
{
99+
return $this->request->getConfig();
100+
}
101+
102+
/**
103+
* Get the result of the http request
104+
*
105+
* @return Response
106+
*/
107+
protected function getResponse()
108+
{
109+
if ($this->response === null) {
110+
$this->response = $this->client->send($this->request);
111+
}
112+
113+
return $this->response;
114+
}
115+
}

‎tests/bootstrap.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22
error_reporting(E_ALL);
33

4+
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
5+
require_once(__DIR__ . '/../vendor/autoload.php');
6+
}
7+
48
include_once dirname(__DIR__).'/src/autoloader.php';
59
include_once __DIR__.'/TestCaseBase.php';
610

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /