【问题标题】:Posting images via wp.uploadFile XML RPC通过 wp.uploadFile XML RPC 发布图像
【发布时间】:2012-02-10 22:25:09
【问题描述】:

我想通过 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"]=&gt; string(84) "http://myblog.com/wp-content/uploads/2012/01/Pein_by_azurewrath87.jpg" ["type"]=&gt; string(10) "image/jpeg" }

但图像是 base64_encodet。如何通过 wp.uploadFile 或 metaWeblog.newPost 方法正确地将图像发送到 wordpress。

【问题讨论】:

    标签: php image wordpress xml-rpc


    【解决方案1】:

    您必须使用 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,
    );
    

    【讨论】:

      【解决方案2】:

      我遇到了完全相同的问题,这里是我用来管理帖子附件的 sn-p,同时跨不同的 wordpress 实例同步帖子。

      如果你想让你测试这个 sn-p,只需设置 $post_to_sync->post_id 一个带有附件的帖子 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> ';
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-26
        • 1970-01-01
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        • 2012-09-15
        • 2014-09-06
        • 2017-01-17
        相关资源
        最近更新 更多