PHP以xml形式獲取POST數據


<?php
namespace Home\Controller;

use Think\Controller;

class UrlController extends Controller
{
    //xml形式獲取post的值curl
    public function send()
    {
        $header[] = 'Content-type: text/xml';//定義content-type為xml
        $xml = '<xml>';
        $xml .= '<name>name</name>';
        $xml .= '<age>14</age>';
        $xml .= '</xml>';

        $url = 'http://localhost/tp/index.php/Home/Url/get';
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//設置鏈接
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設置是否返回信息
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//設置HTTP頭
        curl_setopt($ch, CURLOPT_POST, 1);//設置為POST方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//POST數據
        $response = curl_exec($ch);//接收返回信息
        if (curl_errno($ch)) {//出錯則顯示錯誤信息
            print curl_error($ch);
        }
        curl_close($ch); //關閉curl鏈接
        //$response可以顯示到
        echo $response;//顯示返回信息,
    }

    public function get()
    {
        /*$xmldata = file_get_contents("php://input");
        echo $xmldata;*/

        if($GLOBALS['HTTP_RAW_POST_DATA'])
        echo $GLOBALS['HTTP_RAW_POST_DATA'];
}
}

 

$input = $GLOBALS['HTTP_RAW_POST_DATA'];
    libxml_disable_entity_loader(true);
    $xml = simplexml_load_string($input,'SimpleXMLElement',LIBXML_NOCDATA);
    var_dump($xml);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM