Last active
September 13, 2019 17:26
-
-
Save brandondove/e5dbace973d8184bcc2f65114488a82f to your computer and use it in GitHub Desktop.
Example of using WordPress Core's HTTP API to call a microservice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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