我在使用新的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] =&gt; 1842 [date] =&gt; 2016-05-18T15:08:57 [date_gmt] =&gt; 2016-05-18T15:08:57 [guid] =&gt; stdClass对象 ( [已渲染] =&gt; wordpres.domain /可湿性粉剂内容/上传/ 2016/05 / codeispoetry-RGB-1.png [raw] =&gt; wordpres.domain /可湿性粉剂内容/上传/ 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示例吗? 谢谢大家。
答案 0 :(得分:0)
你好每个人我都解决了这个问题:
$name =$file->getClientOriginalName();
$thumbnailUrl=url('/').$assetPath . '/' .$name;
//start api image upload
$site_url = $site->url."/wp-json/wp/v2/media?access_token=".$token;
// die();
$img=file_get_contents($thumbnailUrl);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $site_url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $img);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Disposition: attachment; filename="'.$name.'"'));
$result=curl_exec ($ch);
谢谢大家