I'm having some trouble converting this function from file_get_contents to wp_remote_get and I was hoping for some insight. This seems like I'm so close. Below are two cases, my original which works using file_get_contents, and the second which does the same thing but uses wp_remote_get and doesn't work. Can anyone help me figure out what I'm screwing up?
case 'one':
$url = "https://api.bitfinex.com/v1/pubticker/btcusd";
$json = json_decode(file_get_contents($url), true);
$price = $json["last_price"];
return $price;
break;
case 'two':
$request = wp_remote_get( 'https://api.bitfinex.com/v1/pubticker/btcusd' );
if( is_wp_error( $request ) ) {
return false;
}
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if( ! empty( $data ) ) {
foreach( $data['last_price'] as $price ) {
return $price;
}
}
break;