【问题标题】:Wordpress XMLRPC (wp.newPost) - Can't get post_date formatted rightWordpress XMLRPC (wp.newPost) - 无法正确格式化 post_date
【发布时间】:2012-12-17 08:22:12
【问题描述】:
    $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 时它仍然不想发布。有人可以帮忙吗,我真的被这个卡住了。

【问题讨论】:

    标签: wordpress datetime xml-rpc


    【解决方案1】:

    想通了:

      $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); 
    

    【讨论】:

      【解决方案2】:

      您可以使用 WordPresse 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 时间戳。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-07
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 2021-11-16
        • 1970-01-01
        • 2021-12-20
        相关资源
        最近更新 更多