【发布时间】:2022-01-19 09:28:06
【问题描述】:
所以,为了解决问题的核心,我想使用 api (v2) 在 wopress 网站上发布一张图片。
问题的第一部分是 我没有 url 或文件路径,我只有 图像的原始数据 在我得到的变量中来自之前完成的导出。
问题的第二部分是一旦发布(正常情况下),图像在管理的媒体库中显示为空白。
这是我的代码:
if (isset($product['priority_web_image'])) {
$image_name = $product['priority_web_image']['filename'];
$data = $product['priority_web_image']['data'];
$ext = substr($image_name, strpos($image_name, ".") + 1);
if ($ext == 'jpg') {
$ext = 'jpeg';
}
$mime_type = 'image/'.$ext;
$headers = [
'Authorization' => 'Bearer '.$result_auth->access_token,
"cache-control" => "no-cache",
"Content-Type" => $mime_type,
"Content-Disposition" => "attachement;filename=".$image_name,
];
$body = [
"source_url" => $data,
"slug" => "image_test_pimcore",
"status" => "future",
"title" => $image_name,
"media_type" => "image",
"mime_type" => $mime_type
];
$options = [
"headers" => $headers,
"form_params" => $body,
];
$result = $this->WPApi->request("POST", "media", $options);
$bodyAry = json_decode($result->getBody());
//echo print_r($bodyAry);
return $bodyAry;
}
我使用 Guzzle 发出请求。
如果有人知道我缺少什么,我会接受它:-)。
【问题讨论】:
标签: php image post command wordpress-rest-api