# Is there any zend-http-client to work with Zend-diactoros-Request (PSR-7 Request)

**URL:** <https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017>\
**Category:** Components & MVC\
**Created:** [April 4, 2019, 4:09pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017 "2019-04-04T16:09:02Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)\
**Post date:** [April 4, 2019, 4:09pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/1 "2019-04-04T16:09:02Z")

</div>

I am trying to call an external API from my `Zend-Expressive` application. I following the documentation ([https://docs.zendframework.com/zend-diactoros/v2/usage/#http-clients](https://docs.zendframework.com/zend-diactoros/v2/usage/#http-clients)). As you can see the `Zend-diactoros` do not ship with an `http-cleint` , are there any Zend-http client that can work with the `Zend-diactoros Request` object.

I know `Zend-http` has a client, but from the documentation, I do not see how it can work with the PSR-7 request object provided found in `Zend-diactoros`, or I may be wrong as I am new to this. If `Zend-http-client` work with `Zend-diactoros-Request` , a code example of that will be greatly appreciated.

P.S.: I am not using the `Zend-http-Request` as that is not PSR-7 compatible.

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [April 4, 2019, 4:21pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/2 "2019-04-04T16:21:15Z")

</div>

Use [https://packagist.org/packages/php-http/httplug](https://packagist.org/packages/php-http/httplug)

---

<div class="post-metadata">

**Author:** ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)\
**Post date:** [April 4, 2019, 5:05pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/3 "2019-04-04T17:05:07Z")

</div>

Hi,  
Thanks for the suggestion. The website is not so user friendly. Can you provide me a code example relevant to expressive showing how can I create a client (for https)?

Thanks

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [April 4, 2019, 5:29pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/4 "2019-04-04T17:29:33Z")

</div>

There’s a link to the repository (and from there to the documentation) 😉

---

<div class="post-metadata">

**Author:** ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)\
**Post date:** [April 4, 2019, 5:50pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/5 "2019-04-04T17:50:47Z")

</div>

I followed that path already… but nothing specific to zend-expressive. I tried myself to come up with something, but no luck yet. As I said, I am new, so an explicit example will be a great help.

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [April 4, 2019, 6:11pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/6 "2019-04-04T18:11:32Z")

</div>

Follow these two guides:

- [http://docs.php-http.org/en/latest/httplug/users.html](http://docs.php-http.org/en/latest/httplug/users.html)

- [http://docs.php-http.org/en/latest/httplug/tutorial.html](http://docs.php-http.org/en/latest/httplug/tutorial.html)

Note that you don’t need `guzzlehttp/psr7`, since diactoros is already a full psr7 implementation.

You’d take the resulting `$client` instance and register it as a service in your application. Something like following would probably suffice (if you use zend-servicemanager):

return [

‘dependencies’ =\> [

‘factories’ =\> [

\Psr\Http\Client\ClientInterface::class =\> [\Http\Discovery\HttpClientDiscovery::class, ‘find’],

]

]

];

This is only off the top of my mind though: please try it out and see if it actually works 🙂

---

<div class="post-metadata">

**Author:** ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)\
**Post date:** [April 4, 2019, 6:15pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/7 "2019-04-04T18:15:59Z")

</div>

Thanks,  
That is a great help. I am going to try it shortly. I hope it will work well in my situation, especially with https://… endpoints.

I also found Gazzle 6 ([http://docs.guzzlephp.org/en/stable/](http://docs.guzzlephp.org/en/stable/)) as a client. What is your opinion on this, how the httpplug is superior to Gazzle?

Thanks again.

---

<div class="post-metadata">

**Author:** ![matthew](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/matthew/32/14_2.png) [@matthew](https://discourse.laminas.dev/u/matthew)\
**Post date:** [April 4, 2019, 6:18pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/8 "2019-04-04T18:18:01Z")

</div>

> [@mmuhasan](#):
>
> but nothing specific to zend-expressive

What do you mean by that, exactly?

- A client made by the ZF project you can use within Expressive?
- A client for making requests to Expressive projects?

In either case, it’s not necessary.

Expressive consumes [PSR-7](https://www.php-fig.org/psr/psr-7/). This means you can use any HTTP client capable of working with PSR-7 within your Expressive application. You can even pass them the current request instance directly!

In the second scenario, any HTTP client can make requests to an Expressive application. This includes (for obvious reasons) your web browser, as well as CLI clients such as cURL and HTTPie. You could also use any PHP-based HTTP client to make requests to your Expressive-based site. The point is: it’s all just HTTP.

> [@mmuhasan](#):
>
> I also found Gazzle 6 ([Guzzle, PHP HTTP client — Guzzle Documentation](http://docs.guzzlephp.org/en/stable/)) as a client. What is your opinion on this, how the httpplug is superior to Gazzle?

http-plug is an interesting project because it provides just a simple interface around using PSR-7 messages to make HTTP requests, and then provides either adapters for userland utilities (libcurl, sockets/streams) or for existing full-featured HTTP clients — including Guzzle. You could use the two of them together if you want to (I’ve done this a few times when working with APIs, so I can provide request middleware).

---

<div class="post-metadata">

**Author:** ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)\
**Post date:** [April 4, 2019, 6:29pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/9 "2019-04-04T18:29:16Z")

</div>

Thanks for your detail explanation.

What here I am trying do is to make an API call from my expressive project. The api will return some json+hal response that I will process at my end. A perfect example of that is foound in [https://docs.zendframework.com/zend-diactoros/v2/usage/#http-clients](https://docs.zendframework.com/zend-diactoros/v2/usage/#http-clients) except it uses an object `$client` that does not ship with the diactoros. So I am looking for a library with which I can create the `client` object. And the API gateway I am talking about is using a secure connection (https://…). And obviously, I am looking for a thin client.

I also looked for HAL client, but not sure there is one out there that can deal with the secure connection.

Does it make any sense now? what do you think?

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [April 4, 2019, 6:47pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/10 "2019-04-04T18:47:20Z")

</div>

> I am looking for a thin client.

That’s what the php-http organisation provides.

---

<div class="post-metadata">

**Author:** ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)\
**Post date:** [April 4, 2019, 8:15pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/11 "2019-04-04T20:15:26Z")

</div>

Okay, I am following your direction. I am using the discovery::find() to get a client. Once I get the client, it will essenntially be one of the installed client (curl, gazzle, etc). I have two questions at this point,

- How can I use this client (in abstract way to send a https request)?
- If there are no way to send the request in an abstract way, then why am I using this.

Am I missing sommething here?

---

<div class="post-metadata">

**Author:** ![ocramius](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ocramius/32/23_2.png) [@ocramius](https://discourse.laminas.dev/u/ocramius)\
**Post date:** [April 4, 2019, 8:27pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/12 "2019-04-04T20:27:07Z")

</div>

see [http://docs.php-http.org/en/latest/httplug/tutorial.html](http://docs.php-http.org/en/latest/httplug/tutorial.html) again. HTTP or HTTPS makes no difference.

---

<div class="post-metadata">

**Author:** ![mmuhasan](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/mmuhasan/32/454_2.png) [@mmuhasan](https://discourse.laminas.dev/u/mmuhasan)\
**Post date:** [April 4, 2019, 9:36pm UTC](https://discourse.laminas.dev/t/is-there-any-zend-http-client-to-work-with-zend-diactoros-request-psr-7-request/1017/13 "2019-04-04T21:36:09Z")

</div>

Okay…I finally able to install it properly. I was almost able to leave it… the tutorial was not so good!!!

Thanks
