サポート » 使い方全般 » XML-RPCでカスタムフィールドの内容を投稿するには?

  • 解決済 qchan

    (@qchan)


    はじめまして。よろしくお願いします。

    XML-RPCでの投稿スクリプトを作りたいのですが、カスタムフィールドの内容をどうすれば送信できるのかで詰まっています。
    どなたか教えていただければ幸いです。

    今回、目的は同一サイト内の管理画面外から行う投稿です。(ログインせずに下書き状態で投稿したい)
    ですので、メール投稿やWordPressの表示画面(PostやPage)に作ったフォームから投稿できれば成功なのです。
    KtaiEntryの作者さまへカスタムフィールドに対応するにはどうすればよいかをお尋ねしようと思っていたところでした。

    調べてみますと、Ver.2.5からはカスタムフィールドをXML-RPCで扱うことが出来るようになったとの事だったのでこれは素晴らしいと思い試行錯誤しています。

    PEARのXML/RPCライブラリを使い、とりあえずタイトル、本文まではエントリーすることができました。

    受け取り側のWordpressスクリプト(xmlrpc.php)には受け取れる関数があります。

    +++++++++++++++++++++++++++++++++++++

    line:1606
    if ( isset($content_struct[‘custom_fields’]) ) {
    $this->set_custom_fields($post_ID, $content_struct[‘custom_fields’]);
    }

    line:247
    function set_custom_fields($post_id, $fields)
    ++++++++++++++++++++++++++++++++++++

    この $content_struct[‘custom_fields’] へ送るためのスクリプトで詰まっています。

    カスタムフィールドを送信するためにはここにあるように、
    http://comox.textdrive.com/pipermail/wp-xmlrpc/2007-November/000103.html

    ` “custom_fields” = (
    {id = 129; key = city; value = Sacramento; },
    {id = 130; key = city; value = Sandy; }
    )`

    のような配列で送ればよいと思うのですが、XML/RPCライブラリに合うように書き換えることが出来ず、
    new XML_RPC_Value() に多次元配列をどう入れるのかわからないまま適当に書いてしまっています。

    PEARのドキュメント
    http://pear.php.net/manual/ja/package.webservices.xml-rpc.api.php

    また、id = foo を書かないと新しく meta_idを振ってくれるとありますのでそのとおりにしました。

    ご指導よろしくお願いします。

    <?php
    require_once("XML/RPC.php"); //XML-RPC package 読み込み
    
    $GLOBALS['XML_RPC_defencoding'] = "UTF-8";
    
    //XML-RPCインタフェース、ユーザ定義
    $mt_host = "xxx.com";//サイトURL
    $mt_xmlrpc_path = "xmlrpc.php";
    $port = 80;
    $mt_user = "xxxx"; // ログインID
    $mt_xmlrpc_passwd = "xxxxxx";//ログインPW
    
    //クライアントの作成
    $myClient = new XML_RPC_client( $mt_xmlrpc_path, $mt_host, $port );
    
    $appkey = new XML_RPC_Value( '', 'string' );
    $username = new XML_RPC_Value( $mt_user, 'string' );
    $passwd = new XML_RPC_Value( $mt_xmlrpc_passwd, 'string' );
    
    //空の配列作成
    $custom_fields = array();
    
    $custom_field['key'] = "company_name";
    $custom_field['value'] = "hoge";
    //カスタムフィールズ(複数のSアリ)に各カスタムフィールドを追加
    array_push($custom_fields, $custom_field);
    
    $content = new XML_RPC_Value(array(
                                        'title'=> new XML_RPC_Value('タイトルが入ります', 'string'),
                                        'description'=> new XML_RPC_Value("本文が入ります", 'string'),
    	                  		        'custom_fields'=> new XML_RPC_Value($custom_fields,'struct')
    
                               							  ), 'struct'
                               						);
    $publish = new XML_RPC_Value( 0, 'boolean' ); //下書きとして投稿
    
    //書き込み
    $myMessage = new XML_RPC_Message(
    'metaWeblog.newPost',
    array($appkey, $username, $passwd, $content, $publish) );
    
    //メッセージ送信
    $myResponse = $myClient->send($myMessage);
    
    $myBlogs = $myResponse->value();
    
    //ブログID取得
    $blogId = $myBlogs->me['string'];
    //ブログID出力
    print_r($blogId);
    
    ?>
2件の返信を表示中 - 1 - 2件目 (全2件中)
  • ‘custom_fields’=> new XML_RPC_Value($custom_fields,’struct’)の部分も分解が必要ではありませんか?
    試したわけではありませんが、$custom_fieldsの配列を生成する際にXML_RPC_Valueしておけばいいような気がします。

    //空の配列作成
    $custom_fields = array();
    
    $custom_fields = new XML_RPC_Value(array(
        "company_name" => new XML_RPC_Value("hoge")), "struct");

    などどうでしょう?
    ちなみに上記ソース

    //空の配列作成
    $custom_fields = array();
    
    $custom_field['key'] = "company_name";
    $custom_field['value'] = "hoge";

    値を入れているときだけfieldsのsが抜けているような・・・
    低レベル回答でごめんなさい;)

    トピック投稿者 qchan

    (@qchan)

    shokun0803さん。ありがとうございます!
    教えて頂いた書き方をもとにやってみますと成功しました!
    使える仕組みになりそうです。感謝いたします。m(_ _)m

    $custom_fields = array();
    
    $custom_fields[] = new XML_RPC_Value(array(
    								'key'=> new XML_RPC_Value('company_name','string'),
    								'value'=> new XML_RPC_Value('hoge','string')
    															),'struct'
    );

    fieldsのsが抜けていたのはforで何件かのエントリーを同時に入れたかったんです。
    書き方おかしいですね。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「XML-RPCでカスタムフィールドの内容を投稿するには?」には新たに返信することはできません。