Skip to content

Instantly share code, notes, and snippets.

@brandondove
Last active September 13, 2019 17:26
Show Gist options
  • Save brandondove/e5dbace973d8184bcc2f65114488a82f to your computer and use it in GitHub Desktop.
Save brandondove/e5dbace973d8184bcc2f65114488a82f to your computer and use it in GitHub Desktop.
Example of using WordPress Core's HTTP API to call a microservice
<?php
/*
* There are many different actions you might
* hook into to trigger a post to your microservice
*/
add_action( 'sample_action', 'post_to_microservice', 10 );
function post_to_microservice() {
$response = wp_remote_post(
'http://your-url.test',
array(
'headers' => array(
/**
* You might include authorization here,
* depending on your microservice's setup
*/
),
'body' => array(
/**
* This is where you would pass any data
* needed for your microservice to consume
*/
'data' => 'Test data',
),
)
);
if ( is_wp_error( $response ) ) {
// Handle the error here
sample_handle_error( $response );
return;
}
// Handle a successful response here
sample_handle_success( $response );
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment