How Do I Make a HTTP Request in WordPress?

Making a HTTP request in WordPress is easy. To make a request to a URL, use the wp_remote_get() function.

The wp_remote_get() function takes two arguments: the URL to request and the callback function to execute when the request is complete. The callback function receives two arguments: the response data and the response code.

To make a request to a URL, use the following syntax:

wp_remote_get(‘http://example.com’, function(data, code) { // code indicates the success or failure of the request });

If the request is successful, the code will be 200 (OK). If the request fails, the code will be one of the following:

404 (Not Found)

500 (Server Error)

The wp_remote_get() function can also be used to retrieve data from a posts/pages/taxonomy term archive. To do this, use the following syntax:

wp_remote_get(‘archive.php?post_type=posts&taxonomy=term&year=2015′, function(data, code) { // code indicates the success or failure of the request });

Conclusion

Making a HTTP request in WordPress is easy. The wp_remote_get() function takes two arguments: the URL to request and the callback function to execute when the request is complete.

If the request is successful, the code will be 200 (OK). If the request fails, the code will be one of the following: 404 (Not Found) 500 (Server Error).