I am currently making a plugin that takes the posts and sends their data to a rest API. Everything else is going smoothly. But I am very confused about how I can achieve this for the featured images. I have to upload a file, not a url. Basically, the API takes multipart/form-data and has an HTTP request method PUT.
The method currently looks something like this:
public function uploadFeaturedImage($post_id)
{
$url = build_api_url('posts/' . $post_id. '/photo');
$featured_image_url = get_the_post_thumbnail_url($post_id);
$image_data = // Get featured image file here
$data = array(
'file' => $image_data
);
$args = array(
'method' => 'PUT',
'headers' => array('Content-Type' => 'multipart/form-data', 'Authorization' => 'Bearer <token>' ),
'body' => $data
);
$response = wp_remote_request($url,$args);
return $response['body'];
}