【发布时间】:2016-09-15 04:28:47
【问题描述】:
我在使用新的 WordPress REST Api 时遇到了一些问题。 我无法使用以下示例上传任何图像。我通过了 Oauth 2 的身份验证,我可以创建帖子、类别,一切都很好。
我的代码是:
$site = Sites::findOrFail($id);
$token=$this->token($site->url,$site->clientId,$site->clientSecret);
$token=$token->getToken();
$site_url = $site->url."/wp-json/wp/v2/media?access_token=".$token;
$options = array (
'http' =>
array (
'ignore_errors' => true,
'method' => 'POST',
'header' =>
array (
1 => 'Content-Type: application/x-www-form-urlencoded',
2 =>'Content-Disposition: attachment; filename="codeispoetry-rgb.png"',
),
'content' =>
http_build_query( array (
'media_urls' => 'https://s.w.org/about/images/logos/codeispoetry-rgb.png',
)),
),
);
$context = stream_context_create( $options );
$response = file_get_contents(
$site_url,
false,
$context
);
$response = json_decode( $response );
echo "<pre>";
print_r($response);
echo "</pre>";
post 方法正常,没有任何错误,响应是这样的:
stdClass Object
( [id] => 1842 [日期] => 2016-05-18T15:08:57 [date_gmt] => 2016-05-18T15:08:57 [guid] => 标准类对象 ( [渲染] => wordpres.domain/wp-content/uploads/2016/05/codeispoetry-rgb-1.png [原始] => wordpres.domain/wp-content/uploads/2016/05/codeispoetry-rgb-1.png )
[modified] => 2016-05-18T15:08:57
[modified_gmt] => 2016-05-18T15:08:57
[password] =>
[slug] => codeispoetry-rgb-1
[status] => inherit
[type] => attachment
[link] => wordpres.domain/codeispoetry-rgb-1/
[title] => stdClass Object
(
[raw] => codeispoetry-rgb-1
[rendered] => codeispoetry-rgb-1
)
[author] => 1
[comment_status] => closed
[ping_status] => closed
[alt_text] =>
[caption] =>
[description] =>
[media_type] => image
[mime_type] => image/png
[media_details] => stdClass Object
(
)
[post] =>
[source_url] => wordpres.domain/wp-content/uploads/2016/05/codeispoetry-rgb-1.png
[_links] => stdClass Object
(
[self] => Array
(
[0] => stdClass Object
(
[href] => wordpres.domain/wp-json/wp/v2/media/1842
)
)
[collection] => Array
(
[0] => stdClass Object
(
[href] => wordpres.domain/wp-json/wp/v2/media
)
)
[about] => Array
(
[0] => stdClass Object
(
[href] => wordpres.domain/wp-json/wp/v2/types/attachment
)
)
[author] => Array
(
[0] => stdClass Object
(
[embeddable] => 1
[href] => wordpres.domain/wp-json/wp/v2/users/1
)
)
[replies] => Array
(
[0] => stdClass Object
(
[embeddable] => 1
[href] => wordpres.domain/wp-json/wp/v2/comments?post=1842
)
)
)
)
但在 WordPress 媒体中: 上传了一张黑色图片,名称添加到: 内容处置:附件;文件名="codeispoetry-rgb.png" 像这样:http://prntscr.com/b5juch
有人可以提供一个经过测试的在 v2 REST API 中发布媒体的 php 示例吗? 谢谢大家。
【问题讨论】: