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

#1

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). 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.

#2

Use https://packagist.org/packages/php-http/httplug

#3

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

#4

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

#5

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.

#6

Follow these two guides:

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 :slight_smile:

#7

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/) as a client. What is your opinion on this, how the httpplug is superior to Gazzle?

Thanks again.

#8

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. 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.

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).

#9

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 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?

#10

I am looking for a thin client.

That’s what the php-http organisation provides.

#11

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?

#12

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

#13

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

Thanks