Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Snippet #007 Get and Post to remote Api with Php. https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/
<?php
/**
* do_remote_get.
*
* Make a get request to a remote api,
*
* @see https://since1979.dev/snippet-007-get-and-post-to-remote-api-with-php/
*
* @uses wp_remote_get() https://developer.wordpress.org/reference/functions/wp_remote_get/
* @uses json_decode() https://www.php.net/manual/en/function.json-decode.php
* @uses wp_remote_retrieve_body() https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/
*
* @param String $url The url/endpoint to call
* @return Array
*/
function do_remote_get(String $url)
{
$response = wp_remote_get($url, array(
'httpversion' => '1.1',
'blocking' => true
));
return json_decode(wp_remote_retrieve_body($response)) ?: [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.