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.

doufei5537
doufei5537 谢谢你的评论。我有-实际上,这就是我为手动cURL请求构建XML的方式。它返回错误#52的事实意味着WordPress完全决定忽略我的连接!我认为可能是一个网络问题,但是它在几个完全没有连接的不同服务器上失败了,这似乎不太可能。
大约 6 年之前 回复
dongxingdu9594
dongxingdu9594 您是否转储了xmlrpc_encode_request()调用的输出以查看它是否与手动构建的XMLRPC匹配?
大约 6 年之前 回复

1个回答



管理以修复它。 查看cURL发送的标头,唯一的区别是cURL命令行使用 Content-Type:application / x-www-form-urlencoded </ code>并且用户代理设置为 User -Agent:curl / 7.30.0 </ code>。</ p>

内容类型的选择不会影响它,但设置用户代理对其进行排序! 似乎WordPress.com(但不是运行最新版本v3.9.2的自托管WordPress.org网站)现在需要一个用户代理来处理XML-RPC请求,尽管这在我找不到的任何地方都没有记录。</ p>

</ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐