Skip to content

Instantly share code, notes, and snippets.

@drrobotnik
Created February 5, 2014 19:41
Show Gist options
  • Save drrobotnik/8831455 to your computer and use it in GitHub Desktop.
Save drrobotnik/8831455 to your computer and use it in GitHub Desktop.
remote_get
function remote_get($key, $url, $args = array(), $expiration = 604800 ){
if( empty( $expiration ) ){
$results = get_option( $key );
}else{
$results = get_transient( $key );
}
if ( false === $results ) {
$response = wp_remote_get($url, $args);
$status = wp_remote_retrieve_response_code( $response );
if($status == 200){
$body = wp_remote_retrieve_body( $response );
if(!$expiration){
update_option( $key, $body );
}else{
set_transient($key, $body, $expiration);
}
return $body;
}else{
return wp_remote_retrieve_response_message( $response );
}
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment