【问题标题】:Wordpress API submit postWordpress API 提交帖子
【发布时间】:2015-05-11 08:08:15
【问题描述】:

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

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

a) 提交一篇文章/帖子并希望

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

从目前的研究来看,我发现您需要登录,这可能是一个两步过程,包括获取随机数,然后使用随机数提交帖子。谁能告诉我在哪里查看 API 文档,或者从哪里开始?

【问题讨论】:

标签: php wordpress curl


【解决方案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 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

【讨论】:

    【解决方案2】:

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

    但是你不需要任何你正在考虑的东西,例如。随机数、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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 2018-04-29
      • 2018-09-21
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 2015-01-16
      相关资源
      最近更新 更多