2009-08-29 24 views
6

幫助,我試圖在WordPress的博客中創建一個新的職位,使用以下perl腳本在XMLRPC上使用metaweblogAPI自定義字段,但似乎有自定義字段的問題。只有第二個自定義字段(寬度)似乎過去了。無法獲得正確發佈的「高度」。當我添加另一個字段時,出現「匿名散列中的奇數元素」錯誤。這應該是簡單的 - 有人會善意地檢查我的語法嗎?謝謝。爲什麼我在Perl中獲得「匿名散列中的奇數個元素」警告?

#!/usr/bin/perl -w 
use strict; 
use RPC::XML::Client; 
use Data::Dumper; 

my $cli=RPC::XML::Client->new('http://www.sitename.com/wp/xmlrpc.php'); 

my $appkey="perl"; # doesn't matter 
my $blogid=1; # doesn't matter (except blogfarm) 

my $username="Jim"; 
my $passwd='_____'; 

my $text=<<'END'; 

This is the post content... 

You can also include html tags... 

See you! 
END 

my $publish=0; # set to 1 to publish, 0 to put post in drafts 

my $resp=$cli->send_request('metaWeblog.newPost', 
$blogid, 
$username, 
$passwd, 
{ 
    'title'  => "this is doodoo", 
    'description' => $text, 
    'custom_fields' => { 
    { "key" => "height", "value" => 500 }, 
    { "key" => "width", "value" => 750 } 
    }, 
}, 
$publish); 

exit 0; 
+0

看到這個複製hashref的答案http://stackoverflow.com/a/7083603/2015531 – ophidion 2013-08-27 09:06:21

回答

13

儘管技術上有效的語法,它並沒有做你的想法。

'custom_fields' => { 
    { "key" => "height", "value" => 500 }, 
    { "key" => "width", "value" => 750 } 
}, 

大致相當於類似:

'custom_fields' => { 
    'HASH(0x881a168)' => { "key" => "width", "value" => 750 } 
}, 

這當然不是你想要的。 (0x881a168部分會有所不同;它實際上是存儲hashref的地址)。

我不確定自定義字段的正確語法是什麼。你可以試試

​​

它將custom_fields設置爲散列數組。但這可能是不對的。這取決於send_request的期望。

+0

CJM,你搖滾!你的建議奏效了。這兩個自定義字段都發布得很好;)非常感謝! – Jim 2009-08-30 03:06:39

相關問題