WordPress API 提交 post
Wordpress API submit post
我是一位经验丰富的 PHP 程序员,熟悉 CURL 并将其与 cookie jar 文件一起使用,并且对 JSON.
也很满意
我不熟悉的是 WordPress 4.1.1,我的目标很简单:本地或通过插件(希望是本地)远程调用 WordPress 站点,并且:
a) 提交 article/post 并希望
b) 获取按用户排序的 posts 列表(以进行比较)。
从目前的研究来看,我发现您需要登录,这可能是一个两步过程,包括获取随机数,然后提交带有随机数的 post。任何人都可以告诉我在 API 文档下哪里可以查看,或者从哪里开始?
您可以使用 XML-RPC API
to do this, here is an simple example using curl
which creates a new post using 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);
要获得 post 的列表,您可以使用 wp.getPosts
,尽管您不能按作者过滤 post,但您可以循环遍历 post响应并检查它是否应该显示:
// 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 了解得足够多,知道比使用它更好。
但是您不需要任何您正在考虑的东西,例如随机数,IXR,XML.
您编写自己的 PHP 脚本。我不明白为什么您需要一个远程博客 post 工具,而网站本质上是可以远程访问的。喜欢使用书签到您的 WP 站点。
我可以看到获取 post 列表的一些可能用途。
为什么您需要安全才能访问供 public 查看的 post?
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');
我是一位经验丰富的 PHP 程序员,熟悉 CURL 并将其与 cookie jar 文件一起使用,并且对 JSON.
也很满意我不熟悉的是 WordPress 4.1.1,我的目标很简单:本地或通过插件(希望是本地)远程调用 WordPress 站点,并且:
a) 提交 article/post 并希望
b) 获取按用户排序的 posts 列表(以进行比较)。
从目前的研究来看,我发现您需要登录,这可能是一个两步过程,包括获取随机数,然后提交带有随机数的 post。任何人都可以告诉我在 API 文档下哪里可以查看,或者从哪里开始?
您可以使用 XML-RPC API
to do this, here is an simple example using curl
which creates a new post using 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);
要获得 post 的列表,您可以使用 wp.getPosts
,尽管您不能按作者过滤 post,但您可以循环遍历 post响应并检查它是否应该显示:
// 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 了解得足够多,知道比使用它更好。
但是您不需要任何您正在考虑的东西,例如随机数,IXR,XML.
您编写自己的 PHP 脚本。我不明白为什么您需要一个远程博客 post 工具,而网站本质上是可以远程访问的。喜欢使用书签到您的 WP 站点。
我可以看到获取 post 列表的一些可能用途。
为什么您需要安全才能访问供 public 查看的 post?
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');