dtmooir3395
2014-08-15 15:35
采纳率: 0%
浏览 106
已采纳

XML-RPC无法通过PHP中的cURL响应POST请求

I'm having some issues with calling WordPress XML-RPC via cURL in PHP. It's a WordPress.com hosted blog, and the XML-RPC file is located at http://sunseekerblogbook.com/xmlrpc.php.

Starting yesterday (or at least, yesterday was when it was noticed), cURL has been failing with error #52: Empty reply from server.

The code snippet we're using is below:

$ch = curl_init('http://sunseekerblogbook.com/xmlrpc.php');
curl_setopt_array($ch, [
    CURLOPT_HEADER => false,
    CURLOPT_HTTPHEADER => [
        'Content-Type: text/xml'
    ],
    CURLOPT_POSTFIELDS => xmlrpc_encode_request('wp.getPosts', [
        1,
        WP_USERNAME,
        WP_PASSWORD,
        [
            'number' => 15
        ]
    ]),
    CURLOPT_RETURNTRANSFER => true
]);
$ret = curl_exec($ch);
$data = xmlrpc_decode($ret, 'UTF-8');

Using cURL directly however, everything returns exactly as expected:

$output = [];
exec('curl -d "<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>wp.getPosts</methodName><params><param><value><int>1</int></value></param><param><value><string>' . WP_USERNAME . '</string></value></param><param><value><string>' . WP_PASSWORD . '</string></value></param><param><value><struct><member><name>number</name><value><int>15</int></value></member></struct></value></param></params></methodCall>" sunseekerblogbook.com/xmlrpc.php', $output);
$data = xmlrpc_decode(implode('', $output), 'UTF-8');

We've been successfully able to query WordPress since July 2013, and we're at a dead-end as to why this has happened. It doesn't look like PHP or cURL have been updated/changed recently on the server, but the first code snippet has failed on every server we've tried it on now (with PHP 5.4+).

Using the http://sunseekerblogbook.wordpress.com/xmlrpc.php link gives the same issue.


Is there anything missing from the PHP code that would cause this issue? That it's suddenly stopped working over 12 months down the line is what has flummoxed me.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxie2613 2014-08-15 16:44
    已采纳

    Managed to fix it. Looking at the headers sent by cURL, the only differences were that the cURL command line uses Content-Type: application/x-www-form-urlencoded and that the user agent was set to User-Agent: curl/7.30.0.

    The choice of content type didn't affect it, but setting a user agent sorted it! It seems WordPress.com (but not self-hosted WordPress.org sites running the latest v3.9.2) now requires a user agent for XML-RPC requests, though this hasn't been documented anywhere that I can find.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题