将图像附加到Wordpress XMLRPC中的帖子

将图像附加到Wordpress XMLRPC中的帖子,wordpress,xml-rpc,Wordpress,Xml Rpc,我正在使用XMLRPC向Wordpress发布帖子。我在发布缩略图时遇到问题,在调试wordpress代码后,我发现我的问题是因为图片没有附加到帖子上。 我必须在不修补wordpress或使用PHP的情况下完成这项工作,只有iwthXMLRPC 我可以上传一张图片并获取图片的ID。 另一点让我困惑的是,你如何将一张图片附加到一篇因为等待图片上传而没有发布的帖子上?我应该先上传图片然后发布,然后使用图片id和发布id更新图片元数据 编辑:wordpress中有问题的代码就是这个检查 if ( $t

我正在使用XMLRPC向Wordpress发布帖子。我在发布缩略图时遇到问题,在调试wordpress代码后,我发现我的问题是因为图片没有附加到帖子上。 我必须在不修补wordpress或使用PHP的情况下完成这项工作,只有iwthXMLRPC

我可以上传一张图片并获取图片的ID。 另一点让我困惑的是,你如何将一张图片附加到一篇因为等待图片上传而没有发布的帖子上?我应该先上传图片然后发布,然后使用图片id和发布id更新图片元数据

编辑:wordpress中有问题的代码就是这个检查

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

我的假设是,它失败是因为图像是未附加的,如果我修复了代码,一切都很好,但我无法修补我的应用程序用户的WP(因此这不是一个解决方案)

是的,可以这样做,如果Wordpress版本是3.5或更高版本,在使用上载文件/图像的代码时,您可以设置post\u id。 我在新帖子中使用的特色图片流程如下:

  • 使用newPost功能,在没有特色内容的情况下发布内容 并将publish设置为false,记录 这个

  • 上传图片并将post_id设置为post的id 发布后,记录图像\u id

  • 完成后,编辑帖子并将wp_post_缩略图设置为 刚上载的图像\u id,并将“发布”设置为true(如果需要)

  • 重要: mime类型很重要,它必须是“image/jpg”或“image/png”,请参阅文档,如果mime类型类似于“jpg”,则附加将失败

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

    这是一个带有类别、图像和标签的帖子示例。它需要类IXR.php

    和mime内容类型函数

    这是我的版本,使用并添加到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 >');
            }
        }
    }
    
    require_once(“IXR_Library.php.inc”);
    $title='My title';
    $body=‘我的身体’;
    $category=“category1,category2”//逗号分隔的预先存在的类别。确保您的博客中存在这些类别。
    $keywords=“关键字1、关键字2、关键字3”;
    $customfields=array('key'=>'Author-bio','value'=>'Autor-bio Here');//以键、值格式插入这样的自定义值
    $title=htmlentities($title,ENT_NOQUOTES,@$encoding);
    $keywords=htmlentities($keywords,entnoquotes,@$encoding);
    $content=array(
    “post_title”=>$title,
    “post_content”=>$body,
    “post\u type”=>“一些自定义的post\u type”,
    “发布状态”=>“草稿”http://codex.wordpress.org/Post_Status
    'mt_allow_comments'=>0,//1允许评论
    'mt_allow_pings'=>0,//1以允许trackback
    “mt_关键字”=>$keywords,
    “类别”=>数组($category),
    “自定义字段”=>数组($customfields)
    );
    //创建客户机对象
    $client=新的IXR_客户端($client)http://example.com/xmlrpc.php');
    $username=“wp_username”;
    $password=“wp\u用户名\u密码”;
    $params=array(0,$username,$password,$content,true);//最后一个参数为“true”,表示立即发布,若要另存为草稿,请将其设置为“false”
    if(!$client->query('wp.newPost',$params)){
    死亡(“
    出现问题-”.$client->getErrorCode()。:“.$client->getErrorMessage()。
    ”); } 其他的 { $post_id=$client->getResponse(); 回显“插入id”。$post\U id; $picture='/full/path/to/pic.jpg'; $content=array( 'name'=>basename($picture), 'type'=>mime\u内容\u类型($picture), 'bits'=>新的IXR_Base64(文件获取内容($picture)), 真的 ); if(!$client->query('metaWeblog.newMediaObject',1,$username,$password,$content)){ 死(“
    出了问题-newMediaObject”“$client->getErrorCode().”:“.$client->getErrorMessage()。
    ”); } 其他的 { $media=$client->getResponse(); $content=array( “发布状态”=>“发布”, “post_缩略图”=>$media['id'] ); if(!$client->query('wp.editPost',0,$username,$password,$post\u id,$content)){ 死(“
    编辑帖子-”.$client->getErrorCode()。:“.$client->getErrorMessage()。
    ”); } } }
    如果只想使用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);
    
    include_once(ABSPATH.WPINC./class IXR.php');
    包括一次(ABSPATH.WPINC./class wp http ixr client.php');
    $usr='username\u在服务器端';
    $pwd='服务器端的密码';
    $xmlrpc='服务器端xmlrpc.php url';
    $client=新的IXR_客户端($xmlrpc);
    ////////////图片上传和附件后创建///////////
    $img_attach='链接到图像';
    $img\u attach\u content=数组(
    'name'=>basename($img_attach),
    'type'=>mime\u内容\u类型($img\u attach),
    'bits'=>新的IXR_Base64(文件获取内容($img_attach)),
    );
    $status=$client->query('wp.uploadFile','1',$usr,$pwd,$img\u attach\u content);
    $image_returnInfo=$client->getResponse();
    ////////////后期创作///////////
    $custom_fields=数组(
    数组('key'=>'blabla1','value'=>'blabla1_value'),
    数组('key'=>'blabla12','value'=>'blabla1_value2')
    ); 
    $post_content=数组(
    “post_type”=>“post”,
    '发布状态'=>'草稿',//暂时
    “post_title”=>“XMLRPC测试”,
    “post_author”=>3,
    “post_name”=>“XMLRPC测试”,
    “post_内容”=>“XMLRPC测试内容”,
    “自定义字段”=>$custom\u字段
    );
    $re
    
    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);