Skip to content
Advertisement

Convert PHP HTTP request to Guzzle

I currently have an old PHP page which carries out a post request to an external API but i am wanting to convert this to Guzzle to tidy it up but im not sure if im on the right lines with this.

PHP POST

JavaScript

Retrieve PHP response

Below is how you retrieved the response where you could make use of the $_POST fields

JavaScript

Guzzle Attempt POST

JavaScript

Guzzle Attempt GET

JavaScript

How would i then grab specific fields from the response?

From my attempts above am i on the right lines?

Advertisement

Answer

If you want to send $vars as a body of POST request, you need to set up body property.

JavaScript

Guzzle can URLencode $vars for you if you use form_params instead of body.

JavaScript

To read server’s response you need to call getBody on the $response and cast it to string. You will have there exactly the same as return value from your original http_post function.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement