PHP8libxml_disable_entity_loader() 弃用、解决微信支付的兼容写法

  public function FromXml($xml) {
    if (!$xml) {
      throw new WxPayException("xml数据异常!");
    }
    //将XML转为array
    //禁止引用外部xml实体
    if (\PHP_VERSION_ID < 80000) {
      libxml_disable_entity_loader(true);
      $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
      return $this->values;
    }else{
      $doc = new DOMDocument();
      $doc->loadXML($xml);
      $xml = $doc->getElementsByTagName( "xml" );
      $result=[];
      foreach( $xml as $k=>$val ){
        foreach($val->childNodes as $v){
          if(!empty($v->tagName)){
            $result[$v->tagName]=$v->nodeValue;
          }
        }
      }
      $this->values=$result;
      return $this->values;
    }


  }
Last modification:April 23, 2023
如果觉得我的文章对你有用,请随意赞赏