• shirotr

    (@shirotr)


    Pear XML-PRCを用いて、PHPからの自動投稿を行いたいのですが、
    カテゴリーの指定が出来なくてこまっております。

    カテゴリー意外に関してはどうさしていますので、指定の仕方に問題ありと思いますので、
    ご存知の方おりましたら、ご教授おねがいします。

    <?php
    //--------------------------------------
    // □ PEARの読み込み
    //--------------------------------------
    $add_path = $_SERVER["DOCUMENT_ROOT"] . "/php/PEAR";
    ini_set( "include_path", $add_path . PATH_SEPARATOR . ini_get( "include_path" ) );
    
    //XML-RPC package
    require("XML/RPC.php");
    //記事本文生成用PHP
    require("wp_blog_autowrite.php");
    
    $GLOBALS['XML_RPC_defencoding'] = "UTF-8";
    
    //WordPress用定義情報
    $wp_blogid = "";
    // //////////////////////////////
    // 変更箇所
    // //////////////////////////////
    //Wordpressが導入されているドメインを記入
    $wp_host = "hogehoge";
    //Wordpressのディレクトリ配下のxmlrpc.phpを記入
    $wp_xmlrpc_path = "/blogs/xmlrpc.php";
    //ブログのログインIDを記入
    $wp_user = "hogehoge";
    //ブログのログインパスワードを記入
    $wp_passwd = "hogehoge";
    //記事情報
    //投稿モード(0:下書記事、1:公開記事)
    $kiji_mode = 1;
    //記事のタイトル
    $kiji_title = $titleItem;
    //記事の本文
    $kiji_honbun =	$contents;
    //記事のカテゴリー
    $categories = array(  //////←問題の箇所です
        new XML_RPC_Value("category name1", "string"),
        new XML_RPC_Value("category name2", "string"),
    );
    // //////////////////////////////
    //以下固定
    $bm = new BlogManager();
    $bm->post_blog($kiji_mode,$kiji_title,$kiji_honbun);
    
    class BlogManager {
        function post_blog($kiji_mode,$kiji_title,$kiji_honbun){
            global $wp_xmlrpc_path,$wp_host,$wp_user,$wp_passwd;
            //クライアントの作成
            echo "クライアント作成<br\n>";
    		$c = new XML_RPC_client($wp_xmlrpc_path, $wp_host, 80); 
    
            //送信データ
            $blogid   = new XML_RPC_Value($wp_blogid, 'string');
            $username = new XML_RPC_Value($wp_user,   'string');
            $passwd   = new XML_RPC_Value($wp_passwd, 'string');
            $content  = new XML_RPC_Value(array(
            'title'      => new XML_RPC_Value($kiji_title,  'string'),
    	'categories' => new XML_RPC_Value($categories,  'array'),  //////←問題の箇所です
            'description'=> new XML_RPC_Value($kiji_honbun, 'string'),
            'dateCreated'=> new XML_RPC_Value(date("Ymd\TH:i:s",time()), 'dateTime.iso8601')
            ), 'struct');
            $publish  = new XML_RPC_Value($kiji_mode, 'boolean');
            //XML-RPCメソッドのセット
            $message = new XML_RPC_Message('metaWeblog.newPost',array($blogid, $username, $passwd, $content, $publish) );
    
            $this->send_message($c,$message);
    
        }
    
        function get_users_blogs(){
            global $wp_xmlrpc_path,$wp_host,$wp_user,$wp_passwd;
            //クライアントの作成
            echo "クライアント作成<br\n>";
            $c        = new XML_RPC_client( $wp_xmlrpc_path, $wp_host, 80 );
            $appkey   = new XML_RPC_Value ( ''         , 'string' );
            $username = new XML_RPC_Value ( $wp_user  , 'string' );
            $passwd   = new XML_RPC_Value ( $wp_passwd, 'string' );
    
            //メッセージ作成
            echo "メッセージ作成<br\n>";
            $message = new XML_RPC_Message( "blogger.getUsersBlogs",array($appkey, $username, $passwd) );
    
            send_message($c,$message);
        }
    
        function send_message($c,$message){
            //メッセージ送信
            echo "メッセージ送信<br\n>";
            $result  = $c->send($message);
    
            if( !$result  ){
                 exit('Could not connect to the server.');
            }else if( $result ->faultCode() ){
                 exit('XML-RPC fault ('.$result ->faultCode().'): '
                     .$result ->faultString());
            }
    
            return $result ;
        }
    }
    ?>
2件の返信を表示中 - 1 - 2件目 (全2件中)
  • nobita

    (@nobita)

    こんにちは、

    解っていない上に、PEARではないんですが、

        $client = new IXR_Client($base_uri);
           if (!$client->query('wp.getCategories','', $USER,$PASS))
            {
                echo('Error occured during category request.' . $client->getErrorCode().":".$client->getErrorMessage());
            }
            $cats = $client->getResponse();

    といった形で、カテゴリリストを取得すると、

    array(71) {
      [0]=>
      array(7) {
        ["categoryId"]=>
        string(3) "138"
        ["parentId"]=>
        string(2) "19"
        ["description"]=>
        string(4) "abby"
        ["categoryDescription"]=>
        string(13) "hello aciform"
        ["categoryName"]=>
        string(4) "abby"
        ["htmlUrl"]=>
        string(34) "http://example.com/wp/?cat=138"
        ["rssUrl"]=>
        string(48) "http://example.com/wp/?feed=rss2&cat=138"
      }
    
      ....
    }

    配列で取得できます。

    $content['title'] = 'Test title ';
            $content['categories'] =  $cats[0];
            if (!$client->query('metaWeblog.newPost','', $USER,$PASS, $content, true))
            {
                die( 'Error while creating a new post' . $client->getErrorCode() ." : ". $client->getErrorMessage());
            }
            $ID =  $client->getResponse();

    という形で、カテゴリを追加できるようです。

    IXR_Clientでも、以下のような形では、カテゴリは追加できませんでした

    $content['categories'] =  array('cat1','cat2');

    どれが必須なのか解りませんが、細かく指定してやる必要があるのかもしれないです。

    トピック投稿者 shirotr

    (@shirotr)

    返信ありがとうございます。
    もう少し調べてみます。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「Pear XML-PRCを用いた、自動投稿のやりかた」には新たに返信することはできません。