使用REST API在wordpress上传图像

soj*_*oju 2 php wordpress

使用rest api我可以创建一个帖子,获取博客类别等但我无法上传我引用的图像
https://github.com/WP-API/client-php/blob/master/library/WPAPI/Media.php

http://wp-api.org/#entities_media-meta_width

我的代码是

$data = array('file'=>$filePath,'is_image'=>true);
print_r($data);

$headers = array('Content-Type' => 'application/octet-stream');
$response = $this->api->post(WPAPI::ROUTE_MEDIA, $headers, $data);
Run Code Online (Sandbox Code Playgroud)

他们谈论[$ response in $ response = $ this-> api-> post(WPAPI :: ROUTE_MEDIA,$ headers,$ data);] $ data中使用的键值对是什么?

soj*_*oju 6

最后我自己解决了

  1. 指定文件路径
  2. 把必要的标题
  3. 发送邮件请求

简单的确定我会在下面显示代码

$filePath = 'URL of image';//right click your fav img and copy url.
$theData = @file_get_contents($filePath);//get image content vvvvvip 
$data = $theData;
$headers = array('Content-Type' => 'application/json; charset= UTF-8',
       'Content-Disposition' => 'attachment; file name=' .base name($filepath),);
$response = $this->api->post('/wp-json/media', $headers,$data);
print_r($response);
Run Code Online (Sandbox Code Playgroud)

//在wordpress媒体中查看它享受..