使用WP-Rest-API v2添加媒体


10

我需要您的帮助,以通过Wp-rest-api v2和Oauth2身份验证在我的wordpress博客中上传媒体图像。

我在REST API文档中没有找到发送图像数据的方式(字段名称,发送模式...?)。

require('OAuth2/Client.php');
require('OAuth2/GrantType/IGrantType.php');
require('OAuth2/GrantType/AuthorizationCode.php');

const CLIENT_ID     = 'XXX';
const CLIENT_SECRET = 'XX';

const REDIRECT_URI           = 'http://127.0.0.1/test_api_wp/test.php';

const AUTHORIZATION_ENDPOINT = 'http://wordpress.local/oauth/authorize';
const TOKEN_ENDPOINT         = 'http://wordpress.local/oauth/token';

$client = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);

if (!isset($_GET['code']))
{
    $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
    header('Location: ' . $auth_url);
    die('Redirect');
}
else
{
    $params = array('code' => $_GET['code'], 'redirect_uri' => REDIRECT_URI);
    $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params); //authorization_code
    $token = $response['result']['access_token'];
    $client->setAccessToken($token);
    $client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_BEARER);

}

$values = array(
    "date" => "2015-11-26 10:00:00",
    "date_gmt" => "2015-11-26 09:00:00",
    "modified" => "2015-11-26 10:00:00",
    "modified_gmt" => "2015-11-26 09:00:00",
    "status" => "future",
    "title" => "Titre media",       
    "description" => "description media",
    "media_type" => "image",
    "source_url" => "https://www.base64-image.de/build/img/mr-base64-482fa1f767.png"
);

$data = $client->fetch("wordpress.local/wp-json/wp/v2/media", $values, "POST");
echo "<pre>";print_r($data);echo "</pre>";

响应 :

Array
(
    [result] => Array
        (
            [code] => rest_upload_no_data
            [message] => No data supplied
            [data] => Array
                (
                    [status] => 400
                )

        )

    [code] => 400
    [content_type] => application/json; charset=UTF-8
)

任何的想法?非常感谢


我已将您评论中的编码添加到问题中。请记住,您可以随时编辑问题以添加更多信息或使其更加清晰。
— cybmeta

多余的内容;在wordpress.local/wp-json/wp/v2/media";这里输入错误,还是在您的真实代码中?
— cybmeta

根据文档,WP REST API v2需要此OAuth插件。我不知道您正在使用的库(OAuth2 / Client.php)是否与WP REST API兼容,但可能与之不兼容。
— cybmeta

非常感谢!额外->; 在我的真实代码中不存在!我使用官方OAuth插件对我进行身份验证,文件OAuth2.Client.php是仅用于轻松进行curl请求的库
— kain34440 2015年

在(documentation)[ v2.wp-api.org/reference/media/]中有一个“创建媒体”部分。我认为您source_url应该是内部post对象。
— ville6000

Answers:


8

所以!这个很有趣。

请记住,WP-API仍在进行中。

内容配置

我发现有关Content-Disposition 的WP-API问题队列上报告了一个问题。这是发布新媒体内容的必需标头,以正确的格式提供媒体时,有一些非常非常严格的要求。

创建媒体端点的目的

首先,让我们退后一步。API假设此时您已经将新文件上传到正确的目录。该端点正在引用该文件的数据库中创建媒体内容。

解决方案

您必须指定媒体文件的文件名才能关联到新内容。这不能是远程URL。如您在v2文档中所见,source_url并且link是只读的。成功提交新内容所需要做的就是在标题中添加以下内容:

'Content-Disposition' => 'filename=name-of-file.jpg',

如票证中所述,您不能添加引号或指定用于发送文件的方法。它必须是上述格式。至少在他们全面改变之前都是这样。

确保文件类型是可接受的文件类型之一,并且您将文件的扩展名包括在请求中。感谢Deo博士的评论。

记录下来,当我终于弄明白这一点时,我洋洋得意地笑着……吓死了我的妻子。


1
大提示与提示Content-Disposition!
— pHiL

我认为这个答案只是一个提示,而不是一个完整的解决方案。我遵循此建议,但我得到了错误Sorry, this file type is not permitted for security reasons
— Brethlosze

@Brethlosze听起来像一个不相关的问题。WordPress在正常的上传过程中会阻止某些类型的媒体。
— MikeNGarrett

3
@Brethlosze文件扩展名必须是可接受的类型之一。例如,这有效,curl --request POST --url http://localhost/kayinjaproject/wp-json/wp/v2/media --header "cache-control: no-cache" --header "content-disposition: attachment; filename=tmp.png" --header "authorization: Basic cm9vdDppYW1haGVybw==" --header "content-type: image/png" --data-binary "@c:/gnu/png.png" --location但是如果您png从文件名中省略tmp.png,则会得到error sorry, this file type is not permitted for security reasons
— Deo Dr Deo'1

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.