WP Rest API上传图片

Cra*_*ath 16 wordpress

我正在尝试通过Wordpress REST api v2上传图像.到目前为止,我所做的只是在wordpress媒体库中创建空条目.意思是他们有图像名称,但没有实际图像.

POST请求:

http://localhost/wordpress/wp-json/wp/v2/media

Authorization: Basic d29yZHByZXNzOndvcmRwcmVzcw==
Content-Type: application/json
Content-Disposition: attachment;filename=map2.jpg

{
  "source_url" : "file:///C:/Users/x/Desktop/map2.jpg"
}
Run Code Online (Sandbox Code Playgroud)

响应:

{
  "id": 127,
  "date": "2016-05-25T08:43:30",
  "date_gmt": "2016-05-25T08:43:30",
  "guid": {
    "rendered": "http://localhost/wordpress/wp-content/uploads/2016/05/map2-3.jpg",
    "raw": "http://localhost/wordpress/wp-content/uploads/2016/05/map2-3.jpg"
  },
  "modified": "2016-05-25T08:43:30",
  "modified_gmt": "2016-05-25T08:43:30",
  "password": "",
  "slug": "map2-3",
  "status": "inherit",
  "type": "attachment",
  "link": "http://localhost/wordpress/map2-3/",
  "title": {
    "raw": "map2-3",
    "rendered": "map2-3"
  },
  "author": 1,
  "comment_status": "open",
  "ping_status": "closed",
  "alt_text": "",
  "caption": "",
  "description": "",
  "media_type": "image",
  "mime_type": "image/jpeg",
  "media_details": {},
  "post": null,
  "source_url": "http://localhost/wordpress/wp-content/uploads/2016/05/map2-3.jpg",
  "_links": {
    "self": [
      {
        "href": "http://localhost/wordpress/wp-json/wp/v2/media/127"
      }
    ],
    "collection": [
      {
        "href": "http://localhost/wordpress/wp-json/wp/v2/media"
      }
    ],
    "about": [
      {
        "href": "http://localhost/wordpress/wp-json/wp/v2/types/attachment"
      }
    ],
    "author": [
      {
        "embeddable": true,
        "href": "http://localhost/wordpress/wp-json/wp/v2/users/1"
      }
    ],
    "replies": [
      {
        "embeddable": true,
        "href": "http://localhost/wordpress/wp-json/wp/v2/comments?post=127"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我没有得到任何错误,一切似乎都在工作,除了response-> post和response-> media_details是null或空.当然图像本身没有上传.

基于此GitHub WP-API添加媒体票证,我应该发送2个请求.第一个POST请求应返回带有post对象的数据.我会通过PUT方法发送这个帖子对象,并且应该上传图片...因为我没有帖子对象,所以这是不可能的.

有什么想法我做错了什么?

rtr*_*oso 17

wordpress api不支持侧载图像,因此您必须进行一些更改.

首先,你的内容类型应该是image/jpeg而不是application/json,请记住,content-type应该反映你传递的数据,而POST媒体请求需要一个图像.

为了适应内容类型,您必须进行的另一项更改是传递数据的方式.不要使用source_url参数发送它,而是尝试将其作为二进制文件传递.

我要提到的最后一件事是wp/v2调用会在几次返回3XX状态.遵循这些重定向并将这些请求重做到这些新URL会很有用.

我有一些问题通过JPEG图像,但PNG图像运行良好.这是我用来上传png媒体的卷曲示例:

curl --request POST \
--url http://www.yoursite.com/wp-json/wp/v2/media \
--header "cache-control: no-cache" \
--header "content-disposition: attachment; filename=tmp" \
--header "authorization: Basic d29yZHByZXNzOndvcmRwcmVzcw==" \
--header "content-type: image/png" \
--data-binary "@/home/web/tmp.png" \
--location
Run Code Online (Sandbox Code Playgroud)

  • @rtrigoso 我可以上传文件,但 WordPress 无法将其识别为图像。我可以看到文件大小约为 300KB。可能有什么问题?PS:我将文件作为 base64 编码的数据字符串发送。 (2认同)

小智 6

对于任何正在寻找 JS 解决方案的人,以下是我如何使用 Axios 使其工作的方法。我将跳过授权实现,因为有一些选项(oAuth、JWT、Basic)。

const fs = require('fs');
const axios = require('axios');

axios({
  url: 'http(s)://{your-wp-domain}/wp-json/wp/v2/media',
  method: 'POST',
  headers: {
    'Content-Disposition':'attachment; filename="file.jpg"',
     Authorization: {your-authorization-method},
    'Content-Type':'image/jpeg'
    },
    data: fs.readFileSync('path/to/file.jpg', (err, data) => {
      if (err) {
        console.log(err);
      }
    });
})
 .then(res => {
   console.log(res.data);
 })
 .catch(err => {
   console.log(err);
 });
Run Code Online (Sandbox Code Playgroud)


小智 5

我使用 PHP cUrl 的工作答案

<?php

$curl = curl_init();

$data = file_get_contents('C:\test.png');

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://woo.dev/wp-json/wp/v2/media",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic XxxxxxxXxxxxXx=",
    "cache-control: no-cache",
    "content-disposition: attachment; filename=test.png",
    "content-type: image/png",
  ),
  CURLOPT_POSTFIELDS => $data,
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
Run Code Online (Sandbox Code Playgroud)