This endpoint /wp/v2/posts
does not return featured image src url. It provides featured image id, that you can use to get image url by performing another REST request. Following function performs a rest request using media id and get image src url.
/**
* Get media url from WP REST API
*
* @param $media_id int
* @return string
*/
function get_image_src_via_rest( $media_id = 0 ) {
$media_id = (int) $media_id;
if ( $media_id < 1 ) {
return '';
}
$response = wp_remote_get( 'https://www.heretheweb.com/blog/wp-json/wp/v2/media/' . $media_id );
// Exit if error.
if ( is_wp_error( $response ) ) {
return '';
}
// Get the body.
$media = json_decode( wp_remote_retrieve_body( $response ) );
return $media->source_url;
}
Now, inside your get_posts_via_rest
function, you can call this new function to grab the featured image url.
$featured_image = get_image_src_via_rest( $post->featured_media );