PHP接收和发送XML数据(json也通用)

一、接收xml数据, 使用php://input,代码如下:

    <?php
        $xmldata=file_get_contents("php://input");
        $data=simplexml_load_string($xmldata);
        print_r($data);
    ?>

二、使用CURL发送xml数据,代码如下:

    <?php
        $xml = file_get_contents('1.xml');
        $url = "http://test.xxx.com/xxx.php";
        $header[]="Content-type: text/xml";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//被弃用了
        $res = curl_exec($ch);
        curl_close($ch);
        
    ?>

 

转:https://blog.csdn.net/wclovesjl/article/details/10348727

 

posted @ 2018-04-01 14:18  与f  阅读(575)  评论(0编辑  收藏  举报