2013-07-18 45 views
6

我正在使用XMLRPC向Wordpress发帖。我在调试WordPress代码后发现有问题发布缩略图,我发现我的问题是由于图片未附加到帖子中导致的。 我必须做到这一点,而不修补wordpress或使用PHP,只能使用XMLRPC。附加图片在Wordpress中发布XMLRPC

我可以上传图片并获取图片的ID。 让我困扰的其他一点是,如何将图片附加到您尚未发布的帖子,因为您等待图片上传?我应该上传图片然后张贴,然后使用图像ID和帖子ID做图像元数据更新?

编辑:在WordPress的代码是有问题的是该检查

if ($thumbnail_html = wp_get_attachment_image($thumbnail_id, 'thumbnail')) 

和我的假设是,它失败了,因为图像是独立的,如果我修复代码一切都很好,但我不能补丁我的应用程序用户的WP(所以这不是一个解决方案)

+0

请参阅http://wp.tutsplus.com/tutorials/creative-coding/uploading-pictures-via-xml-rpc-and-php-to-wordpress/。 – user1929959

+0

这是不可能的,没有以某种方式操纵PHP代码。 –

回答

7

是的,这是可能的,如果WordPress的版本是3.5或更高,当使用代码上传文件/图像,你可以设置post_id。 我用于与特色照片新帖的流量是这样的:

  1. 使用newPost功能和发布的内容没有特色 图像,也可以设置发布到假,记录由 这

    返回POST_ID
  2. 上传的图片和POST_ID设置为只 张贴的帖子的ID,当完成编辑后记录image_id

  3. ,并设置wp_post_thumbnail等于 image_id您刚刚上传,也可以设置发布到真(如果需要)

重要: MIME类型是重要的,它必须是“图像/ JPG”或“图像/ PNG”请参阅文件,如果MIME类型是worng,就像“JPG”附加将失败。

提示: 对于调试,如果你从wordpress得到一个通用的错误,你不知道为什么你可以检查wordpress代码,甚至编辑它,添加调试/跟踪调用,希望你能找出原因。

这是带有类别,图像和标签的帖子的示例。它要求类IXR.php
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-IXR.php
和mime_content_type功能
https://github.com/caiofior/storebaby/blob/master/magmi/plugins/extra/general/socialnotify/wp/mimetype.php

 $client = new IXR_Client($url); 
     $content = array(
      'post_status' => 'draft', 
      'post_type' => 'post', 
      'post_title' => 'Title', 
      'post_content' => 'Message', 
      // categories ids 
      'terms' => array('category' => array(3)) 
     ); 
     $params = array(0, $username, $password, $content); 
     $client->query('wp.newPost', $params); 
     $post_id = $client->getResponse(); 

     $content = array(
      'name' => basename('/var/www/sb/img.jpeg'), 
      'type' => mime_content_type('/var/www/sb/img.jpeg'), 
      'bits' => new IXR_Base64(file_get_contents('/var/www/sb/img.jpeg')), 
      true 
     ); 
     $client->query('metaWeblog.newMediaObject', 1, $username, $password, $content); 
     $media = $client->getResponse(); 
     $content = array(
      'post_status' => 'publish', 
      // Tags 
      'mt_keywords' => 'tag1, tag2, tag3', 
      'wp_post_thumbnail' => $media['id'] 
     ); 
     $client->query('metaWeblog.editPost', $post_id, $username, $password, $content, true); 
+0

为什么有必要通过两个步骤来编辑?在我看来,你可以先上传图片,捕获id,然后在newPost()中添加Id? –

+0

@RickStrahl当时相信我,所有这些步骤都很重要,现在可能会有所不同,但要上传该图片,您必须拥有post_id才能将图片附加到帖子,并设置您需要的精选图片的ID上传的图片,我的问题和答案是直接使用XMLRPC,没有WP库可能可以用更少的步骤来完成 – simion314

0

这是我的版本,使用wp.newPostwp.editPost,加在的WordPress 3.4,中允许使用的自定义后类型

require_once("IXR_Library.php.inc"); 
$title = 'My title'; 
$body = 'My body'; 
$category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog. 
$keywords="keyword1, keyword2, keyword3"; 
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format 

$title = htmlentities($title,ENT_NOQUOTES,@$encoding); 
$keywords = htmlentities($keywords,ENT_NOQUOTES,@$encoding); 

$content = array(
    'post_title'=>$title, 
    'post_content'=>$body, 
    'post_type'=>'some_custom_post_type', 
    'post_status' => 'draft', // http://codex.wordpress.org/Post_Status 
    'mt_allow_comments'=>0, // 1 to allow comments 
    'mt_allow_pings'=>0, // 1 to allow trackbacks 
    'mt_keywords'=>$keywords, 
    'categories'=>array($category), 
    'custom_fields' => array($customfields) 
); 

// Create the client object 
$client = new IXR_Client('http://example.com/xmlrpc.php'); 
$username = "wp_username"; 
$password = "wp_username_password"; 

$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false' 

if (!$client->query('wp.newPost', $params)) { 
    die('<br/><strong>Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >'); 

    } 
else 
{ 
    $post_id = $client->getResponse(); 
    echo 'Inserted with id'.$post_id; 
    $picture = '/full/path/to/pic.jpg'; 
    $content = array(
     'name' => basename($picture), 
     'type' => mime_content_type($picture), 
     'bits' => new IXR_Base64(file_get_contents($picture)), 
     true 
    ); 
    if (!$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content)) { 
     die('<br/><strong>Something went wrong - newMediaObject'.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >'); 
    } 
    else 
    { 
     $media = $client->getResponse(); 
     $content = array(
      'post_status' => 'publish', 
      'post_thumbnail' => $media['id'] 
     ); 
     if (!$client->query('wp.editPost', 0, $username, $password, $post_id, $content)) { 
      die('<br/><strong>Something went wrong editPost - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >'); 
     } 
    } 
} 
1

我的版本,如果你只想使用wp.newPost和WP。editPost

include_once(ABSPATH . WPINC . '/class-IXR.php'); 
include_once(ABSPATH . WPINC . '/class-wp-http-ixr-client.php'); 

    $usr = 'username_on_the_server_side'; 
    $pwd = 'password_on_the_server_side'; 
    $xmlrpc = 'server side xmlrpc.php url'; 
    $client = new IXR_Client($xmlrpc); 

    //////////// IMAGE UPLOAD AND ATTACHMENT POST CREATION /////////// 
     $img_attach = 'link to the image'; 
     $img_attach_content = array(
       'name' => basename($img_attach), 
       'type' => mime_content_type($img_attach), 
       'bits' => new IXR_Base64(file_get_contents($img_attach)), 
         ); 
     $status = $client->query('wp.uploadFile','1', $usr, $pwd, $img_attach_content); 
     $image_returnInfo = $client ->getResponse(); 

    //////////// POST CREATION /////////// 

     $custom_fields = array( 
          array('key' => 'blabla1', 'value' => 'blabla1_value'), 
          array('key' => 'blabla12', 'value' => 'blabla1_value2') 
         ); 
     $post_content = array(
      'post_type' => 'post', 
      'post_status' => 'draft', //for now 
      'post_title' => 'XMLRPC Test', 
      'post_author' => 3, 
      'post_name' => 'XMLRPC Test', 
      'post_content' => 'XMLRPC Test Content', 
      'custom_fields' => $custom_fields 
     ); 

    $res = $client -> query('wp.newPost',1, $usr, $pwd, $post_content); 
    $postID = $client->getResponse(); 
    if(!$res) 
     echo 'Something went wrong....'; 
    else { 
      echo 'The Project Created Successfully('.$res.')<br>Post ID is '.$postID.'<br>'; 
    } 

    //////////// Image Post Attachment Edit /////////// 
     $img_attach_content2 = array(
       'post_type' => 'attachment', 
       'post_status' => 'inherit', 
       'post_title' => $postID, 
       'post_name' => $postID, 
       'post_parent' => $postID, 
       'guid' => $image_returnInfo['url'], 
       'post_content' => '', 
       'post_mime_type' => 'image/jpg' 
       ); 

    $res2 = $client -> query('wp.editPost', 0, $usr, $pwd,  $image_returnInfo['id'], $img_attach_content2); 

    $postIDimg = $client->getResponse();  

    //////////// POST EDIT /////////// 

     $post_content2 = array(
       'post_status' => 'publish', //publish 
       'wp_post_thumbnail' => $image_returnInfo['id'], 
       'custom_fields' => array('key' => '_thumbnail_id', 'value' => $image_returnInfo['id']) 
      ); 
      $media2= $client->query('wp.editPost',0, $usr, $pwd, $postID, $post_content2);