我是一位经验丰富的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

关于php - Wordpress API提交帖子,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28954370/

10-16 20:40