Wordpress API 提交帖子


Wordpress API submit post

我是一个经验丰富的PHP程序员,熟悉CURL并将其与cookie jar文件一起使用,并且对JSON也很熟悉。

我不熟悉的是WordPress 4.1.1,我的目标很简单:远程调用WordPress网站,无论是本地还是通过插件(希望是本机的),并且:

a) 提交文章/帖子,希望

b) 获取按日期排序的用户帖子列表(进行比较)。

从到目前为止的研究中,我看到您需要登录,也许这是一个两步过程,包括获取随机数,然后提交带有随机数的帖子。 谁能告诉我在 API 文档下查找何处,或从哪里开始?

您可以使用

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

要获取您可以使用的帖子列表,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

我对WP有足够的了解,比使用它更了解。

但是你不需要任何你正在考虑的东西,例如nonce,IXR,XML。

你编写自己的PHP脚本。 我不明白为什么当网站本质上是远程访问时,您需要远程博客文章工具。 就像在您的WP网站上使用书签一样。

我可以看到获取帖子列表的一些可能用途。

为什么您需要安全性才能访问供公众查看的帖子?

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";

从浏览器访问:

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

从远程 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

如果不想将数据副本保存在文件中

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