WP Rest API 上传图片 [英] WP Rest API upload image

查看:80
本文介绍了WP Rest API 上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to upload image via Wordpress REST api v2. So far all I managed was to create empty entries in wordpress media library. Meaning they have image names, but no actual image.

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"
}

回复:

{
  "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"
      }
    ]
  }
}

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

I get no errors, everything's seem to be working, except response->post and response->media_details is either null or empty. Ofcourse image itself is not uploaded.

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

Based on this GitHub WP-API Adding Media ticket, I should send 2 requests. First POST request should return data with post object. I would send this post object via PUT method, and image should be uploaded...since I have no post object, this is not possible.

知道我做错了什么吗?

推荐答案

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

Sideloading images is not supported by the wordpress api so you will have to do some changes.

首先,您的 content-type 应该是 image/jpeg 而不是 application/json,请记住 content-type 应该反映您正在传递的数据,并且 POST 媒体请求需要一个图像.

First, your content-type should be image/jpeg and not application/json, remember that content-type is supposed to reflect the data that you are passing and the POST media request expects an image.

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

Another change you have to make to accommodate the content-type is the way that you are passing the data. Instead of sending it with the source_url parameter, try passing it as a binary file.

我要提到的最后一件事是 wp/v2 调用有时会返回 3XX 状态.跟踪这些重定向并将这些请求重做到这些新 url 会很有用.

One last thing I would mention is that the wp/v2 calls return 3XX status on a few occasions. It would be useful to follow those redirects and redo those requests to those new urls.

我在传递 JPEG 图像时遇到了一些问题,但 PNG 图像运行良好.这是我用来上传 png 媒体的 curl 示例:

I had some issues passing JPEG images but PNG images have worked well. Here is a curl example that I use to upload png media:

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

这篇关于WP Rest API 上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆