You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

服务器向URL发送rcode、lcode参数但代码无法接收求助

问题:PHP无法接收XML-RPC推送的rcode和lcode参数

服务器日志确认已推送参数:

Dec 12 16:41:54 x1 wcron: [-] postNtimes: Posting done, url= b'wubook.revroo.io/push-notification';, params= {'rcode': 1670859713, 'lcode': 1669807740}

按照文档配置推送通知后,我的Laravel代码无法获取到rcode和lcode参数,代码如下:

public function url(Request $request)
{
    $lcode = $request->has('lcode') ? $request->lcode : null;
    $rcode = $request->has('rcode') ? $request->rcode : null;

    $xml = "<?xml version='1.0' encoding='UTF-8'?>
<methodResponse>
    <param>
        <value>
        <int>" . $lcode . "</int>
        </value>
    </param>
    <param>
        <value>
        <int>" . $rcode . "</int>
        </value>
    </param>
</methodResponse>";
    $response = simplexml_load_string($xml);
    return response([$lcode, $rcode], 200);
    return xmlrpc_decode($response);
}

请求接收情况截图


原因与解决方案

核心问题

服务器是通过XML-RPC协议推送参数的,而你直接用$request->xxx获取参数——Laravel的Request对象默认只解析表单、JSON这类常规格式的请求体,不会自动解析XML-RPC的XML内容,所以拿不到参数。

修复步骤

  1. 读取原始XML请求体
    直接从php://input获取未解析的XML内容:
$rawXml = file_get_contents('php://input');
  1. 解析XML-RPC参数
    用PHP内置的xmlrpc_decode函数解析XML,得到参数数组:
$params = xmlrpc_decode($rawXml);
// 从解析后的数组中提取参数,根据实际结构调整
$lcode = $params['lcode'] ?? null;
$rcode = $params['rcode'] ?? null;
  1. 修正响应逻辑
    你的代码里有两个return语句,第二个永远不会执行。另外XML-RPC响应需要返回XML格式内容,并且设置正确的Content-Type头。

修改后的完整代码

public function url(Request $request)
{
    // 获取原始XML请求体
    $rawXml = file_get_contents('php://input');
    // 解析XML-RPC参数
    $params = xmlrpc_decode($rawXml);

    // 提取参数,处理可能的空值
    $lcode = $params['lcode'] ?? null;
    $rcode = $params['rcode'] ?? null;

    // 构建标准XML-RPC响应
    $xmlResponse = "<?xml version='1.0' encoding='UTF-8'?>
<methodResponse>
    <param>
        <value>
            <int>" . ($lcode ?? 0) . "</int>
        </value>
    </param>
    <param>
        <value>
            <int>" . ($rcode ?? 0) . "</int>
        </value>
    </param>
</methodResponse>";

    // 返回XML响应,设置正确的Content-Type
    return response($xmlResponse, 200)->header('Content-Type', 'text/xml');
}

额外检查项

  • 确认服务器推送的是标准XML-RPC格式,而非其他格式
  • 如果是Laravel项目,需将该路由加入VerifyCsrfToken中间件的$except数组,避免CSRF验证拦截请求

内容的提问来源于stack exchange,提问作者Lalarukh khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 12:55:22