我目前正在制作一个插件,用于接收帖子并将其数据发送到rest API。其他一切都进展顺利。但我很困惑我如何才能为特色图片做到这一点。我必须上传一个文件,而不是url。基本上,API接受多部分/表单数据,并有一个HTTP请求方法PUT。
该方法当前看起来如下所示:
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\'];
}