最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

xml rpc - XMLRPC won't connect?

programmeradmin1浏览0评论

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
  • Is the request definitely making it through to the origin server and not e.g. serving a cached response from the edge server? e.g. check the access logs. Can you make your XMLRPC requests directly to the origin server instead? – Rup Commented Oct 7, 2020 at 12:38
  • @Rup confirmed, I do see an entry in the access log for each request: POST /xmlrpc.php HTTP/1.1" 200 4435 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0" – AAA Commented Oct 7, 2020 at 12:49
  • Would it not be easier to use the REST API? Keep in mind that the REST API is capable of doing more, doesn't require XML to parse, and is available in more places, whereas XMLRPC is disabled on a lot of hosts for security reasons. Also are you sure that's the response? It looks more like the request – Tom J Nowell Commented Oct 7, 2020 at 13:04
  • Looking at the code for IXR_Client that is most likely the request not the response, it displays both assuming there were no issues. What's the value of $res? Are you sure it wouldn't be easier to make a HTTP POST to example/wp-json/wp/v2/posts with the basic auth plugin? – Tom J Nowell Commented Oct 7, 2020 at 13:08
  • @TomJNowell maybe I should switch to HTTP POST - is this the REST API approach? Do you have sample code that would do a post with this approach? I havent heard of it until this post... – AAA Commented Oct 7, 2020 at 15:09
 |  Show 3 more comments

1 Answer 1

Reset to default 2

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/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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论