我想通过XML RPC将图片批量上传到我的wordpress博客,然后通过img标签将图片放到wordpress帖子中。
但是我的wordpress和wp.uploadFile没有返回base64编码文件,而不是有效的图像。
这是我的php代码。
<?php $q = new IXR_Client('http://myblog.com/xmlrpc.php');
$mediaarray = array(
"name" => $image_name,
"type" => $atrybuty[mime],
"bits" => base64_encode($file),
"overwrite" => false,
);
if(!$q->query('wp.uploadFile', 1, $uzyt, $has, $mediaarray)){
echo $q->getErrorCode().': '.$q->getErrorMessage();
}
var_dump($q->getResponse());响应是
array(3) { ["file"]=> string(24) "Pein_by_azurewrath87.jpg" ["url"]=> string(84) "http://myblog.com/wp-content/uploads/2012/01/Pein_by_azurewrath87.jpg" ["type"]=> string(10) "image/jpeg" }
但图像是base64_encodet。如何正确地通过wp.uploadFile或metaWeblog.newPost方法将图片发送到wordpress。
发布于 2012-01-18 20:59:58
您必须使用IXR_Base64( data )将数据转换为实际的数据对象,而不仅仅是包含base64内容的字符串。
<?php $q = new IXR_Client('http://myblog.com/xmlrpc.php');
$mediaarray = array(
"name" => $image_name,
"type" => $atrybuty[mime],
"bits" => new IXR_Base64($file),
"overwrite" => false,
);发布于 2012-12-12 17:06:12
我遇到了完全相同的问题,这里是我在不同的wordpress实例中同步帖子时用来管理帖子附件的代码片段。
如果想要测试这段代码,只需将$ post _ to _sync->post_id设置为带有附件的post ID:
/****************************BEGIN ATTACHMENTS****************************/
//get attachments from the original content
$attachments = & get_children( array(
'post_parent' => $post_to_sync->post_id, //replace here with a post id
'post_type' => 'attachment',
));
if ( $attachments != array() ) {
foreach ( $attachments as $attachment_id => $attachment ) {
$params = array(
0,
XMLRPC_USER,
XMLRPC_PWD,
array(
'name' => basename( get_attached_file( $attachment_id ) ), //$attachment->post_title,
'type' => $attachment->post_mime_type,
'bits' => new IXR_Base64 ( file_get_contents ( get_attached_file( $attachment_id ) ) ),
'post_parent' => $id_int,
)
);
$client->query('metaWeblog.newMediaObject',$params) ;
echo '<br> <br> ';
var_dump($client->getResponse());
echo '<br> <br> ';echo '<br> <br> ';echo '<br> <br> ';
}
}https://stackoverflow.com/questions/8872216
复制相似问题