Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress API submit post

I am an experienced PHP programmer, familiar with CURL and using it with cookie jar file, and also comfortable with JSON.

What I am not familiar with is WordPress 4.1.1, and my goal is simple: remotely call a WordPress site either natively or by a plugin (hopefully natively), and:

a) submit an article/post and hopefully

b) get a list of posts by user sorted by date as well (to compare).

From research so far I see you need to be logged in, and perhaps it is a 2-step process including getting a nonce and then submitting the post with the nonce. Can anyone tell me where to look under API documentation, or where to start?

like image 871
Oliver Williams Avatar asked Oct 19 '22 16:10

Oliver Williams


2 Answers

You could use the 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);

To get a list of posts you may use wp.getPosts, although you cannot filter posts by author, you could loop through each post in the response and check if it should be displayed or not:

// 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
like image 151
Cyclonecode Avatar answered Oct 22 '22 21:10

Cyclonecode


I know enough about WP to know better than to use it.

But you do not need any of the stuff you are considering e.g. nonce, IXR, XML.

You write your own PHP script. I do not understand why you need a remote blog post tool when websites by their nature are remotely accessible. Like use a bookmark to your WP site.

I can see some possible uses for getting a list of posts.

Why would you need security to access posts that are there for the public to see?

Script on WP site:

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";

Access from Browser:

http://wp_site.com/script.php?date=m/d/y'

Script accessed from remote PHP script:

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

If you do not want to save a copy of data in file

header('Content-Type: text/plain; charset=utf-8');
echo file_get_contents('http://wp_site.com/script.php?date=m/d/y');
like image 20
Misunderstood Avatar answered Oct 22 '22 22:10

Misunderstood