zoukankan      html  css  js  c++  java
  • curl发送xml , xml和数组互转

    public function postXml($url, array $data)
        {
            // pack xml
            $xml = $this->arrayToXml($data);
    
            // curl post
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($ch, CURLOPT_HEADER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
            $response = curl_exec($ch);
            if (!$response) {
                throw new Exception('CURL Error: ' . curl_errno($ch));
            }
            curl_close($ch);
    
            // unpack xml
            return $this->xmlToArray($response);
        }
    
        public function arrayToXml(array $data)
        {
            $xml = "<xml>";
            foreach ($data as $k => $v) {
                if (is_numeric($v)) {
                    $xml .= "<{$k}>{$v}</{$k}>";
                } else {
                    $xml .= "<{$k}><![CDATA[{$v}]]></{$k}>";
                }
            }
            $xml .= "</xml>";
            return $xml;
        }
    
        public function xmlToArray($xml)
        {
            return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        }
    

      

  • 相关阅读:
    java实现HTTP请求 HttpUtil
    java-websocket客户端 断线重连 注入Service问题
    人工智能博客
    git 修改注释
    2019-2-22
    2019-2-21
    2019-2-20
    /与./和../的含义
    第二章(构建有多个房间的聊天室程序)
    第一章(欢迎进入node.js世界)
  • 原文地址:https://www.cnblogs.com/freespider/p/7764819.html
Copyright © 2011-2022 走看看