Do Remote Post Curl Request with Wordpress Print

  • 3

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"];


Was this answer helpful?

« Back