Uso para trabalhar com API de sites de terceiros, (MailChimp, Google, Instagram…).
/**
* HTTP request
* via https://code.difluir.com/
* info https://developer.wordpress.org/reference/functions/wp_remote_post/
*/
$email = '';
$api_key = '';
$endpoint = 'https://api.site.com/';
$response = wp_remote_post( $endpoint, array(
'method' => 'GET',
'headers' => array(
'Authorization' => 'Basic '.base64_encode( 'user:'. $api_key )
),
'body' => json_encode( array(
'email_address' => $email
) )
) );
//info https://developer.wordpress.org/reference/functions/wp_remote_retrieve_response_code/
if ( ! wp_remote_retrieve_response_code($response) == 200 ) {
return false;
}
//info https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/
$data = wp_remote_retrieve_body($response);
if ( $data && $data !== false ) {
return json_decode($data);
}