@wilmer.lemke
To make an HTTP request using PHP in a WordPress plugin, you can utilize the wp_remote_get() function provided by WordPress. This function allows you to retrieve the content of a given URL using the HTTP GET method. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Define the URL you want to make a request to $url = 'https://example.com/api/endpoint'; // Make the HTTP GET request $response = wp_remote_get( $url ); // Check if the request was successful if ( is_wp_error( $response ) ) { // Handle the error $error_message = $response->get_error_message(); echo "Something went wrong: $error_message"; } else { // Get the response body $body = wp_remote_retrieve_body( $response ); // Process the response as needed echo "Response: $body"; } |
This example demonstrates making an HTTP GET request to the specified URL. If the request is successful, the response body is retrieved using wp_remote_retrieve_body()
function, and you can then process it as needed. If an error occurs, it can be handled using the is_wp_error()
function.
Note that wp_remote_get()
supports various arguments that you can use to customize the request headers, timeouts, and more. Check the WordPress documentation for wp_remote_get()
for more details on available options.