我需要您的帮助,以通过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
多余的内容
—
cybmeta
;
在wordpress.local/wp-json/wp/v2/media";
这里输入错误,还是在您的真实代码中?
非常感谢!额外->; 在我的真实代码中不存在!我使用官方OAuth插件对我进行身份验证,文件OAuth2.Client.php是仅用于轻松进行curl请求的库
—
kain34440 2015年