通过PHP从另一台服务器发布到WordPress

时间:2016-01-22 作者:Julian F

我在上运行了一个wordpress网站,比如“site.com”和另一个位于完全不同位置的网站“input.com”。顾名思义,“input.com”应该用一个非常简单的形式(例如:标题和内容)作为输入网站。然后输入的信息应该发送到“site.com”并作为博客帖子发布,就像有人使用wp管理面板输入信息一样。

通常我会使用cURL来实现这一点,但由于wp管理面板非常复杂(登录和其他),我寻找了其他方法来实现它。不幸的是,wordpress rest API似乎仅限于从“site.com”获取数据,但没有向其发送数据的选项。我发现的所有其他API都不能正常工作(或根本不能正常工作),因为它们要么已经过时,要么文档记录不良。

有没有什么简单的方法可以不用编写cURL程序就做到这一点?到目前为止,我发现最有希望的事情是:https://github.com/HarriBellThomas/Wordpress_PostController但我还没能让它起作用。我真的很感激你的帮助!

编辑:

<?php

set_time_limit(0);
error_reporting(E_ALL);
ini_set(\'display_errors\', 1);

include("./wp-includes/post.php");

// Create post object
$my_post = array(
  \'post_title\'    => "test",//wp_strip_all_tags( $_POST[\'post_title\'] ),
  \'post_content\'  => "teeeeeeeessssssssssttttttttttt",// $_POST[\'post_content\'],
  \'post_status\'   => \'publish\',
  \'post_author\'   => 1,
  \'post_category\' => array( 1,2 )
);

// Insert the post into the database
wp_insert_post( $my_post );

?>

2 个回复
最合适的回答,由SO网友:Julian F 整理而成

我最终按照丹尼斯的建议,使用XML-RPC完成了这项任务。斯托亚诺夫。如果服务器上安装了xmlrpc,只需使用wpPostXMLRPC function 来自stackoverflow。但请记住xmlrpc_encode_request() 是一个你通常会试图避免的实验函数。

SO网友:jdm2112

在站点上设置脚本。com接收来自输入的数据。com和使用wp_insert_post() 在网站上创建帖子。com。

标题、内容和其他帖子详细信息使用以下数组结构发送:

$post = array(
  \'ID\'             => [ <post id> ] // Are you updating an existing post?
  \'post_content\'   => [ <string> ] // The full text of the post.
  \'post_name\'      => [ <string> ] // The name (slug) for your post
  \'post_title\'     => [ <string> ] // The title of your post.
  \'post_status\'    => [ \'draft\' | \'publish\' | \'pending\'| \'future\' | \'private\' | custom registered status ] // Default \'draft\'.
  \'post_type\'      => [ \'post\' | \'page\' | \'link\' | \'nav_menu_item\' | custom post type ] // Default \'post\'.
  \'post_author\'    => [ <user ID> ] // The user ID number of the author. Default is the current user ID.
  \'ping_status\'    => [ \'closed\' | \'open\' ] // Pingbacks or trackbacks allowed. Default is the option \'default_ping_status\'.
  \'post_parent\'    => [ <post ID> ] // Sets the parent of the new post, if any. Default 0.
  \'menu_order\'     => [ <order> ] // If new post is a page, sets the order in which it should appear in supported menus. Default 0.
  \'to_ping\'        => // Space or carriage return-separated list of URLs to ping. Default empty string.
  \'pinged\'         => // Space or carriage return-separated list of URLs that have been pinged. Default empty string.
  \'post_password\'  => [ <string> ] // Password for post, if any. Default empty string.
  \'guid\'           => // Skip this and let Wordpress handle it, usually.
  \'post_content_filtered\' => // Skip this and let Wordpress handle it, usually.
  \'post_excerpt\'   => [ <string> ] // For all your post excerpt needs.
  \'post_date\'      => [ Y-m-d H:i:s ] // The time post was made.
  \'post_date_gmt\'  => [ Y-m-d H:i:s ] // The time post was made, in GMT.
  \'comment_status\' => [ \'closed\' | \'open\' ] // Default is the option \'default_comment_status\', or \'closed\'.
  \'post_category\'  => [ array(<category id>, ...) ] // Default empty.
  \'tags_input\'     => [ \'<tag>, <tag>, ...\' | array ] // Default empty.
  \'tax_input\'      => [ array( <taxonomy> => <array | string>, <taxonomy_other> => <array | string> ) ] // For custom taxonomies. Default empty.
  \'page_template\'  => [ <string> ] // Requires name of template file, eg template.php. Default empty.
);  
将ID留空以创建新帖子。

WP Codex的详细文档:https://codex.wordpress.org/Function_Reference/wp_insert_post