Do Remote Post Curl Request with Wordpress

If you need to do a CURL request within the context of a WordPress plugin or Theme, then the code below is the simplest.

For additional examples, please check: https://developer.wordpress.org/reference/functions/wp_remote_post/

Basic Request
This would be for a basic request.

<?php
###################################
# CURL with Wordpress
# See: https://www.jucra.com/whmcs/knowledgebase/177/
###################################
$data = array( 
     'key1' => 'value1', 
     'key2' => 'value2' 
);
$response = wp_remote_post( 'http://httpbin.org/post', array( 'data' => $data ) );


Request With Auth Headers

You can add your auth headers if that's required of the remote API.

<?php
###################################
# CURL with Wordpress
# See: https://www.jucra.com/whmcs/knowledgebase/177/
###################################
$data = array( 
     'key1' => 'value1', 
     'key2' => 'value2'
);
$response = wp_remote_post( 'http://httpbin.org/post', 
     array(
          'body'    => $data,
          'headers' => array(
          'Autorisation' => $api_key,
          ),
     )
);

Then to grab the $response use $reponse["body"];

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

customised wordpress wp-config file

Out of the box, the WordPress wp-config.php file is basic and ugly.Below is our standard...

Fix WordPress 404 Errors on Password Protected Directories

You are reading this article becasue you have applied password protection on the wp-admin...

Enable Fenced Off Debugging to Your IP in Wordpress

Use the code below to activate the bugging in WordPress but locked down to your IP.Put this in...

How to protect your Wordpress Login from Bots

You are reading this article because you are getting a lot of attacks on your WordPress login...

SVG Logo is not appearing in Wordpress

You are reading this article because you have managed to upload an SVG file to your Wordpress...