Wordpress api post 图像原始数据在媒体库中不为空白
Wordpress api post image raw data without being blank in media library
所以,为了直截了当,我想 post 在 worpress 网站上使用 api (v2) 的图像.
问题的第一部分是 我没有 url 或文件路径 ,我只有 原始数据image 在我从之前完成的导出中获得的变量中。
问题的第二部分是,posted(正常情况下)后,图像 在管理的媒体库中显示为空白。
这是我的代码:
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 来发出请求。
如果有人知道我缺少什么,我会接受它:-)。
我找到了解决方案!
file_put_contents($filepath, base64_decode($data));
// Make sure the image exist
if (!file_exists($filepath)){return;}
// Load the image
$file = file_get_contents($filepath);
// Get the filename
$filename = $image_name? $image_name : basename($filepath);
// Initiate curl.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_URL, $url .'/wp-json/wp/v2/media/' );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
"Content-Disposition: form-data; filename=\"$filename\"",
'Authorization: Bearer ' .$result_auth->access_token,
] );
$result = curl_exec( $ch );
curl_close( $ch );
// Decode the response
$api_response = json_decode($result);
return $api_response;
所以,为了直截了当,我想 post 在 worpress 网站上使用 api (v2) 的图像.
问题的第一部分是 我没有 url 或文件路径 ,我只有 原始数据image 在我从之前完成的导出中获得的变量中。
问题的第二部分是,posted(正常情况下)后,图像 在管理的媒体库中显示为空白。
这是我的代码:
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 来发出请求。
如果有人知道我缺少什么,我会接受它:-)。
我找到了解决方案!
file_put_contents($filepath, base64_decode($data));
// Make sure the image exist
if (!file_exists($filepath)){return;}
// Load the image
$file = file_get_contents($filepath);
// Get the filename
$filename = $image_name? $image_name : basename($filepath);
// Initiate curl.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_URL, $url .'/wp-json/wp/v2/media/' );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
"Content-Disposition: form-data; filename=\"$filename\"",
'Authorization: Bearer ' .$result_auth->access_token,
] );
$result = curl_exec( $ch );
curl_close( $ch );
// Decode the response
$api_response = json_decode($result);
return $api_response;