代码之家  ›  专栏  ›  技术社区  ›  memme

使用PHP远程发布到WordPress博客(最佳解决方案)

  •  -1
  • memme  · 技术社区  · 7 年前

    我正在寻找最好的解决方案,从另一台服务器上的PHP脚本发布到WordPress博客。 有没有已经开发好的PHP脚本? 我认为它不会像wp rest api那样使用cookie认证模型?

    非常感谢你

    当做

    梅梅

    1 回复  |  直到 7 年前
        1
  •  0
  •   memme    7 年前

    认为最好的解决方案是将WordPress的XML-RPC与此PHP客户端一起使用: https://github.com/letrunghieu/wordpress-xmlrpc-client

    以下是添加新的WordPress博客帖子和检索网址的代码:

        require_once 'WordpressClient.php';
        require_once('.\Exception\NetworkException.php');
        require_once('.\Exception\XmlrpcException.php');
    
        $endpoint = "http://www.example.com/xmlrpc.php";
        $wpUser = 'username';
        $wpPass = 'password';
        $YourCategoryID = 5;
        $wpClient = new \HieuLe\WordpressXmlrpcClient\WordpressClient();
        $wpClient->setCredentials($endpoint, $wpUser, $wpPass);
        $title="Your Blog Post Title";
        $title = htmlentities( $title, ENT_NOQUOTES, 'UTF-8' );
        $body='Your HTML coded article';
        $content = array(
        'post_category' => array( $YourCategoryID ), // my category id
        'post_type' => 'post',
        'post_status' => 'published',
        'post_title' => $title,
        'post_content' => $body,
        'ping_status' => 'closed',
        'comment_status' => 'closed',
    );
    
        $result=$wpClient->newPost($title,$body,$content);
        $postname=$wpClient->getPost($result);
        $new_post_url_slug=$postname['post_name'];