php curl get post json xml请求提交

做做笔记,有错误,望大神指点,多多讨论,多多留言!

arrayToXml($data);
        }

        $curl = curl_init();
        if ($https) {                                                       //设置为https请求,不验证证书和hosts
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        // 设置超时
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);//严格校验

        // 设置header
        curl_setopt($curl, CURLOPT_HEADER, false);
        // 要求结果为字符串且输出到屏幕上
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        // post提交方式
        curl_setopt($curl, CURLOPT_POST, true);
//        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
        // 运行curl
        $response_data = curl_exec($curl);
        $status = curl_getinfo($curl);
        curl_close($curl);

        if(intval($status["http_code"])==200){
            return $response_data;
        }else{
            return  false;
        }
    }

    /**
     * 数组转xml字符
     * @param array $data
     * @return bool|string
     */
    public function arrayToXml($data=array()){
        if(!is_array($data) || count($data) <= 0){
            return false;
        }
        $xml = "";
        foreach ($data as $key=>$val){
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."";
            }else{
                $xml.="<".$key.">";
            }
        }
        $xml.="";
        return $xml;
    }

    /**
     * xml转json字符串
     * @param $xml
     * @param bool $isFile
     * @return bool|false|string
     */
    public function xmlToString($xml,$isFile=false){
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        if($isFile){
            if(!file_exists($xml)) return false;
            $xmlStr = file_get_contents($xml);
        }else{
            $xmlStr = $xml;
        }
        $result= json_encode(simplexml_load_string($xmlStr, 'SimpleXMLElement', LIBXML_NOCDATA));
        return $result;
    }

}

你可能感兴趣的:(php,Laravel)