In this tutorial, I will show you how to use Guzzle in a WordPress plugin to make HTTP requests.

Step 1: Install Guzzle
The first thing you need to do is install Guzzle in your WordPress plugin. You can do this using Composer, the PHP dependency management tool.
In your terminal, navigate to your WordPress plugin folder and run the following command:
composer require guzzlehttp/guzzle
This will download and install Guzzle in your WordPress plugin.
Step 2: Create a PHP file to make HTTP requests
Once you have Guzzle installed in your WordPress plugin, it is time to create a PHP file to make HTTP requests. You can create a new PHP file in your WordPress plugin folder and name it “http-request.php”.
In the “http-request.php” file, you will need to add the following code:
<?php
require_once('/path/to/vendor/autoload.php'); // Reemplaza '/path/to/vendor/autoload.php' con la ruta correcta a tu archivo autoload.php
use GuzzleHttp\Client;
$client = new Client();
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/posts/1');
echo $response->getBody();
?>
In this code, we are including Guzzle's autoload.php file and then creating a new Guzzle client object. Next, we send a GET request to the URL https://jsonplaceholder.typicode.com/posts/1 and store the response in the variable `$response`. Finally, we print the response body using the “getBody()” method.
Step 3: Add a shortcode to display the response
Once you have created the “http-request.php” file, it is time to add a shortcode in WordPress to display the response on a page or post.
To do this, you can add the following code to your WordPress theme's functions.php file:
function display_http_response() {
ob_start();
include('http-request.php');
return ob_get_clean();
}
add_shortcode('http_response', 'display_http_response');
This code defines a function called “display_http_response()” that loads the “http-request.php” file and returns its content using the “ob_get_clean()” method. Then, we add a shortcode called “[http_response]” that calls this function.
Step 4: Test the shortcode
Once you have added the shortcode, you can test it by creating a new page or post in WordPress and adding the shortcode “[http_response]”. This should display the response from the HTTP request you made in the “http-request.php” file.
Conclusion
Using Guzzle in a WordPress plugin is a useful way to make HTTP requests and retrieve data from external services. By following the steps described in this tutorial, you will be able to install Guzzle in your WordPress plugin, create a PHP file to make HTTP requests, add a shortcode to display the response, and test the shortcode. If you encounter connection issues, be sure to review the Guzzle documentation to resolve them. Good luck!
Do you need help with a web project?
Do you need help with a web project? Don't hesitate to contact me. I develop complete and customized solutions with WordPress and PHP, using modern tools and processes, HTML, CSS, SCSS, PHP, JavaScript, Bootstrap, and more. Ready? Send me a message and let's talk about your web project!
