XMLRPC won't connect?

I am using the latest version of Wordpress and trying to run a simple PHP script to create a wordpress post but unfortunately it isn't working. I am using Cloudflare plus have a webhost that is providing an SSL certificate if it matters.

There are no errors in the PHP logs and nothing unusual in the Cloudflare logs... I am very confused where this is breaking :(

PHP script:

$usr = 'ABCXYZ';
$pwd = 'ABCXYZ';
$xmlrpc = 'https://www.ABCXYZ.com/xmlrpc.php';
$client = new IXR_Client($xmlrpc);
$category = 'ABCXYZ';
//$tag = 'ABCXYZ';

$client - debug = true; //optional but useful

$params = array(
    'post_type' = 'post',
    //publish
    'post_status' = 'publish',
    'post_title' = $title,
    'post_author' = 1,
    'comment_status'= 'open',
    'terms_names'  = array( 
            'post_tag' = array( $tag ), 
            'category' = array( $category )),
    //'post_excerpt' = 'This is my test test',
    'post_content' = $content
);

$res = $client - query('wp.newPost',1, $usr, $pwd, $params);

Response:

POST /xmlrpc.php HTTP/1.0
Host: www.ABCXYZ.com
Content-Type: text/xml
User-Agent: wp-iphone/4.8.1 (iPhone OS 8.1.3, iPad) Mobile
Content-Length: 1163

?xml version=1.0?
methodCall
methodNamewp.newPost/methodName
params
paramvalueint1/int/value/param
paramvaluestringABCXYZ/string/value/param
paramvaluestringABCXYZ/string/value/param
paramvaluestruct
  membernamepost_type/namevaluestringpost/string/value/member
  membernamepost_status/namevaluestringpublish/string/value/member
  membernamepost_title/namevaluestringtest title2/string/value/member
  membernamepost_author/namevalueint1/int/value/member
  membernamecomment_status/namevaluestringopen/string/value/member
  membernameterms_names/namevaluestruct
  membernamepost_tag/namevaluearraydata
  valuestringtest tag/string/value
/data/array/value/member
  membernamecategory/namevaluearraydata
  valuestringABCXYZ/string/value
/data/array/value/member
/struct/value/member
  membernamepost_content/namevaluestringtest content/string/value/member
/struct/value/param
/params/methodCall

Topic cloudflare xml-rpc Wordpress

Category Web


Instead of using XMLRPC which isn't available on some hosts, use the REST API instead.

Send a HTTP POST request to the posts endpoint containing a JSON object with your desired post, with an authentication header.

To do this, we're going to need an authentication plugin ( standard WP only supports nonce + cookie which isn't useful for an external app ).

First, install the JSON Basic auth plugin https://github.com/WP-API/Basic-Auth

With this we can now do this on a remote WP site to create posts:

$response = wp_remote_post(
    'https://example.com/wp-json/wp/v2/posts',
    [
        'headers' => [
            'Authorization' => 'Basic ' . base64_encode( 'username:password' )
        ],
        'body' => [
            'title'   => 'Post Title',
            'status'  => 'publish',
            'content' => 'Hello World!',
        ],
    ]
);

if ( wp_remote_retrieve_response_message( $response ) === 'Created' ) {
    echo 'success!';
}

Likewise we can use other tools, e.g. here is the same example written as a curl command for the command line:

curl --user admin:password -X POST -H "Content-Type: application/json" -d "{title:'Post Title',status:'publish',content:'hello world'}" https://example.com/wp-json/wp/v2/posts

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.