Oli*_*ams 5 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);
Run Code Online (Sandbox Code Playgroud)
要获取您可以使用的帖子列表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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1407 次 |
| 最近记录: |