php8 函数 ‘libxml_disable_entity_loader‘ 已弃用 xml转数组

原代码:
//禁止引用外部xml实体
libxml_disable_entity_loader(true);

新代码:

        // 设置 libxml 以禁用外部实体加载
        libxml_set_external_entity_loader(function ($public, $system, $context) {
            // 可以抛出异常、return 等操作
            throw new BusinessException(Code::PARAM_ERROR, "禁止加载外部实体");
        });

PHP XML转数组

    /**
     * xml转数组
     * @param $xml
     * @return mixed
     * @throws BusinessException
     */
    public function toArray($xml)
    {
        header("Content-Type: text/html;charset=utf-8");

        // 设置 libxml 以禁用外部实体加载
        libxml_set_external_entity_loader(function ($public, $system, $context) {
            throw new BusinessException(Code::PARAM_ERROR, "禁止加载外部实体");
        });

        // 加载 XML 数据并转换为数组
        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }

PHP数组转XML

    /**
     * 数组转XML
     * @param $data
     * @param $type
     * @return string
     * @throws BusinessException
     */
    private function toXml($data, $type = 'xml'): string
    {
        header("Content-Type: text/html;charset=utf-8");
        if (!is_array($data) || count($data) <= 0) {
            throw new BusinessException(Code::PARAM_ERROR, "数组数据异常!");
        }
        $xml = "<$type>";
        foreach ($data as $key => $val) {
            $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
        }
        $xml .= "</$type>";
        return $xml;
    }
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值