Posting images via wp.uploadFile XML RPC

自作多情 提交于 2019-12-08 12:19:03

问题


I want to mass upload images to my wordpress blog via XML RPC and then put images in to wordpress post via img tag.

But my wordpress and wp.uploadFile not return base64 encodet file instead of valid image.

Here is my php code.

<?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());

response is

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" }

But image is base64_encodet. How to proper send image to wordpress via wp.uploadFile or metaWeblog.newPost method.


回答1:


You have to use IXR_Base64(data) to convert the data to an actual data object, and not just a string with base64 content.

<?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,
);



回答2:


I encountered exactly the same issue, here the snippet I use to manage post attachments while synchronizing posts accrosss different instances of wordpress.

If want you to test this snippet, just set $post_to_sync->post_id with a post ID which has attachments :

    /****************************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/posting-images-via-wp-uploadfile-xml-rpc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!