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 = '.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
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>
<methodName>wp.newPost</methodName>
<params>
<param><value><int>1</int></value></param>
<param><value><string>ABCXYZ</string></value></param>
<param><value><string>ABCXYZ</string></value></param>
<param><value><struct>
<member><name>post_type</name><value><string>post</string></value></member>
<member><name>post_status</name><value><string>publish</string></value></member>
<member><name>post_title</name><value><string>test title2</string></value></member>
<member><name>post_author</name><value><int>1</int></value></member>
<member><name>comment_status</name><value><string>open</string></value></member>
<member><name>terms_names</name><value><struct>
<member><name>post_tag</name><value><array><data>
<value><string>test tag</string></value>
</data></array></value></member>
<member><name>category</name><value><array><data>
<value><string>ABCXYZ</string></value>
</data></array></value></member>
</struct></value></member>
<member><name>post_content</name><value><string>test content</string></value></member>
</struct></value></param>
</params></methodCall>
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/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
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>
<methodName>wp.newPost</methodName>
<params>
<param><value><int>1</int></value></param>
<param><value><string>ABCXYZ</string></value></param>
<param><value><string>ABCXYZ</string></value></param>
<param><value><struct>
<member><name>post_type</name><value><string>post</string></value></member>
<member><name>post_status</name><value><string>publish</string></value></member>
<member><name>post_title</name><value><string>test title2</string></value></member>
<member><name>post_author</name><value><int>1</int></value></member>
<member><name>comment_status</name><value><string>open</string></value></member>
<member><name>terms_names</name><value><struct>
<member><name>post_tag</name><value><array><data>
<value><string>test tag</string></value>
</data></array></value></member>
<member><name>category</name><value><array><data>
<value><string>ABCXYZ</string></value>
</data></array></value></member>
</struct></value></member>
<member><name>post_content</name><value><string>test content</string></value></member>
</struct></value></param>
</params></methodCall>
Share
Improve this question
edited Oct 7, 2020 at 16:22
fuxia♦
107k38 gold badges255 silver badges459 bronze badges
asked Oct 7, 2020 at 12:36
AAAAAA
1251 silver badge7 bronze badges
8
|
Show 3 more comments
1 Answer
Reset to default 2Instead 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/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/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/wp-json/wp/v2/posts
$res
? Are you sure it wouldn't be easier to make a HTTP POST toexample/wp-json/wp/v2/posts
with the basic auth plugin? – Tom J Nowell ♦ Commented Oct 7, 2020 at 13:08