Skip to content

Instantly share code, notes, and snippets.

@rileypaulsen
Created August 23, 2014 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rileypaulsen/b58d582def11564b2b54 to your computer and use it in GitHub Desktop.
Save rileypaulsen/b58d582def11564b2b54 to your computer and use it in GitHub Desktop.
Use authentication with the JSON REST API
//Note: requires the Basic Auth plugin
$username = 'username';
$password = 'password';
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username . ':' . $password ),
),
);
$queryFilters = array(
'posts_per_page'=>-1,
's'=>'search query',
'tax_query' => array(
array(
'field'=>'slug',
'taxonomy'=>'designation',
'terms'=>array('staff','student','alumni')
)
)
);
$query = '?';
foreach($queryFilters as $key => $value):
$query .= "filter[$key]=".maybe_serialize($value)."&";
endforeach;
echo '<pre>';
print_r($query);
echo '</pre>';
$employees = wp_remote_get(get_json_url().'/posts/'.$query, $args);
echo '<pre>';
print_r(json_decode($employees['body']));
echo '</pre>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment