php - Wordpress API 提交帖子

标签 php wordpress curl

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

相关文章:

php - 在 CentOS 上将 PHP 5.3 更新到 PHP 5.5 后,我无法访问 phpmyadmin

PHP如何读取子目录和循环文件?

php - 将 php 表单链接到数据库

wordpress - WooCommerce - 覆盖运费

python - 下载Google Play上存在的YouTube视频

php - 如何使用 cURL 在 PHP 中将我的代码转换为同步 http 请求

shell - 如何通过curl获取GitHub OAuth2状态码来模拟Web Application Flow?

php - 打开 Office 进行 doc、docx 和 rtf 到 html 的转换

html - tm 符号不会像在 html 中那样显示,sm 符号可以工作

javascript - 如何将 JavaScript 小部件添加到 Wordpress.com 托管的博客中?