简体   繁体   English

Wordpress API提交帖子

[英]Wordpress API submit post

I am an experienced PHP programmer, familiar with CURL and using it with cookie jar file, and also comfortable with JSON. 我是一位经验丰富的PHP程序员,熟悉CURL并将其与cookie jar文件一起使用,并且对JSON也很满意。

What I am not familiar with is WordPress 4.1.1, and my goal is simple: remotely call a WordPress site either natively or by a plugin (hopefully natively), and: 我不熟悉的是WordPress 4.1.1,我的目标很简单:本地或通过插件(希望是本地)远程调用WordPress网站,并且:

a) submit an article/post and hopefully a)提交文章/帖子,并希望

b) get a list of posts by user sorted by date as well (to compare). b)还获得按日期排序的用户帖子列表(以进行比较)。

From research so far I see you need to be logged in, and perhaps it is a 2-step process including getting a nonce and then submitting the post with the nonce. 从目前的研究来看,我认为您需要登录,这可能是一个两步过程,包括获取一个随机数,然后与随机数一起提交帖子。 Can anyone tell me where to look under API documentation, or where to start? 谁能告诉我在API文档下应该去哪里查找或从哪里开始?

You could use the XML-RPC API to do this, here is an simple example using curl which creates a new post using wp.newPost : 您可以使用XML-RPC API来执行此操作,这是一个使用curl的简单示例,该示例使用wp.newPost创建新帖子:

// initialize curl
$ch = curl_init();
// set url ie path to xmlrpc.php
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/xmlrpc.php");
// xmlrpc only supports post requests
curl_setopt($ch, CURLOPT_POST, true);
// return transfear
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// setup post data
$content = array(
  'post_type' => 'post',
  'post_content' => 'This is the post content',
  'post_title' => 'This is the post title',
  'post_status' => 'publish',
);
// parameters are blog_id, username, password and content
$params = array(1, '<user>', '<password>', $content);
$params = xmlrpc_encode_request('wp.newPost', $params);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
// execute the request
curl_exec($ch);
// shutdown curl
curl_close($ch);

To get a list of posts you may use wp.getPosts , although you cannot filter posts by author, you could loop through each post in the response and check if it should be displayed or not: 要获取帖子列表,您可以使用wp.getPosts ,尽管您不能按作者过滤帖子,但是您可以循环浏览响应中的每个帖子并检查是否应显示它:

// filter used when retrieving posts
$filter = array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'number' => 50,
  'offset' => 0,
  'orderby' => 'post_title',
);
// fields to include in response
$fields = array(
  'post_title',
  'post_author',
  'post_id',
  'post_content',
);
$params = array(1, '<username>', '<password>', $filter, $fields);
$params = xmlrpc_encode_request('wp.getPosts', $params);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
// execute query
$response = curl_exec($ch);
// response is xml
$response = simplexml_load_string($response);
// walk over response and figure out if post should be displayed or not

I know enough about WP to know better than to use it. 我对WP的了解足够多,比使用它更好。

But you do not need any of the stuff you are considering eg nonce, IXR, XML. 但是您不需要考虑任何东西,例如随机数,IXR,XML。

You write your own PHP script. 您编写自己的PHP脚本。 I do not understand why you need a remote blog post tool when websites by their nature are remotely accessible. 我不明白为什么可以从本质上远程访问网站时需要使用远程博客发布工具。 Like use a bookmark to your WP site. 喜欢使用书签到您的WP网站。

I can see some possible uses for getting a list of posts. 我可以看到获取帖子列表的一些可能用途。

Why would you need security to access posts that are there for the public to see? 您为什么需要安全性才能访问公开显示的帖子?

Script on WP site: WP网站上的脚本:

header('Content-Type: text/plain; charset=utf-8');
$rows = 0;
$date = date('Y-m-d',strtotime($_GET['date'])) . '00:00:00';
$results=mysqli_query("SELECT`comment_post_ID`,`comment_date`,`comment_content`  
  FROM `wp_comments` WHERE `comment_date` > '$date' 
  ORDER BY `comment_post_ID` ASC,`comment_date` ASC);
while ($pats = mysqli_fetch_array($results, MYSQL_NUM)){
  echo "$row[0]\t$row[1]\r\n";
}
echo "$rows\trows\n";

Access from Browser: 从浏览器访问:

http://wp_site.com/script.php?date=m/d/y'

Script accessed from remote PHP script: 从远程PHP脚本访问的脚本:

header('Content-Type: text/plain; charset=utf-8');
$data = file_get_contents('http://wp_site.com/script.php?date=m/d/y');
$fp = fopen('posts.csv');
fwrite($fp,$data);
fclose($fp);
echo $data

If you do not want to save a copy of data in file 如果您不想在文件中保存数据副本

header('Content-Type: text/plain; charset=utf-8');
echo file_get_contents('http://wp_site.com/script.php?date=m/d/y');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM