Wordpress XMLRPC(wp.newPost) - 无法正确格式化post_date

时间:2012-12-17 08:22:12

标签: wordpress datetime xml-rpc

    $content = array(
        'post_title' => $title,
        'post_content' => $body,
        'post_status' => 'publish', 
        'post_date' => $pub_date //What's the proper format for the date here?
    )

    $params = array(0,$this->settings['username'],$this->settings['password'],$content);
    $request = xmlrpc_encode_request('wp.newPost',$params);
    $this->Curl->post($this->controller->rpc_url,$request); 

我为post_date格式尝试了很多不同的变体,但没有一个有效。以下是我已经尝试过的所有组合,但没有一个组合起作用:

1) $pub_date = date('Y-m-d H:i:s', time());
2) $pub_date = time();
3) $pub_date = new IXR_Date(time());
4) $pub_date = date('c',time());
5) $datetime = new DateTime('2010-12-30 23:21:46');
   $pub_date = $datetime->format(DateTime::ISO8601);

似乎我测试了所有可能的解决方案,并且每当我尝试包含post_date时它仍然不想发布。有人可以请求帮助,我真的被困在这个。

2 个答案:

答案 0 :(得分:2)

想出来:

  $publish_date = '20121217T01:47:03Z' //this is the proper format for datetime 
  xmlrpc_set_type($publish_date, 'datetime'); //xmlrpc_set_type must be used on above date so that XML passes it properly as <dateTime.iso8601> instead of <string>

    $content = array(
        'post_title' => $title,
        'post_content' => $body,
        'post_status' => 'publish', 
        'post_date' => $publish_date);

    $params = array(0,$this->settings['username'],$this->settings['password'],$content);
    $request = xmlrpc_encode_request('wp.newPost',$params);
    $this->Curl->post($this->controller->rpc_url,$request); 

答案 1 :(得分:0)

您可以使用WordPress Core上提供的代码:

require '/ROOT/maaal/wp-includes/class-IXR.php';

$timestamp = time(); // Or some value you calculated somewhere
$xmlrpc_date = new IXR_Date($timestamp);

PS :$ time可以是PHP时间戳或ISO时间戳。