Skip to content

Instantly share code, notes, and snippets.

@Farmatique
Created August 29, 2019 19:35
Show Gist options
  • Save Farmatique/4ee707844ec3ba6f6a4209293f48e4ae to your computer and use it in GitHub Desktop.
Save Farmatique/4ee707844ec3ba6f6a4209293f48e4ae to your computer and use it in GitHub Desktop.
Wordpress php get JSON data from API fetch data
<?php
$url = "https://endpoint.api";
$username = "username";
$password = "password";
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password )
)
);
$response = wp_remote_get($url, $args);
$body = wp_remote_retrieve_body( $response );
$api_response = json_decode( $body );
?>
<div class="api-response">
<?php print_r($api_response);//returns Array ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment