Wordpress API提交帖子

时间:2015-03-10 00:47:31

标签: php wordpress curl

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

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

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

b)按用户排序的帖子列表(按比较)。

从目前为止的研究中我发现你需要登录,也许这是一个两步的过程,包括获取一个nonce,然后用nonce提交帖子。任何人都可以告诉我在API文档下查看哪里,或从哪里开始?

2 个答案:

答案 0 :(得分:1)

您可以使用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 retreiving 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);
// excute 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

答案 1 :(得分:0)

我对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');