php
  • xml
  • linux
  • wget
  • 2013-03-23 52 views -1 likes 
    -1
    wget -d --header="Content-Type:application/xml" --post-data="$(cat <your xml file>)" http://sample.sample.com/api 
    

    如何在php中使用此函數? 我也想得到這個函數的響應。 我在PHP中可行的一個變量是這樣如何將此wget函數轉換爲php?

    $xml = '<sample> 
        <Request target="test"> 
        </Request> 
    </sample>' 
    

    這是我要發佈的XML。

    我試過如下:

    $url = 'sample.sample.com/api';; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, "$(cat <".$xml.">)"); // receive server response ... 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    echo $server_output = curl_exec ($ch); 
    curl_close ($ch); 
    

    但它返回此錯誤:

    Parsing XML failed: Start tag expected, '<' not found

    +2

    你試過PHP捲曲對象? – georgecj11 2013-03-23 14:13:29

    +0

    我不知道如何使用捲曲功能像這樣的wget功能......如果你知道如何使捲曲這樣工作,它將是偉大的 – 2013-03-23 14:15:40

    +1

    這個問題的重複:http://stackoverflow.com/questions/3080146/post -data-to-url-php – 2013-03-23 14:16:56

    回答

    1

    你應該能夠遵循類似的情景如在Sending and Receiving XML using PHP所示。該站點的第二部分(發送XML)使用curl來處理與wget具有類似屬性的操作,但使用PHP庫而不是命令行二進制文件和參數。我將包含此網站的長片段。

    <?php 
        /* 
        * XML Sender/Client. 
        */ 
        // Get our XML. You can declare it here or even load a file. 
        $xml_builder = ' 
            <?xml version="1.0" encoding="utf-8"?> 
            <Test> 
             <String>I like Bharath.co.uk!</String> 
            </Test> 
           '; 
        // We send XML via CURL using POST with a http header of text/xml. 
        $ch = curl_init('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); 
        curl_setopt($ch, CURLOPT_HEADER, 0); 
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
        curl_setopt($ch, CURLOPT_REFERER, 'http://www.bharath..co.uk'); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        $ch_result = curl_exec($ch); 
        curl_close($ch); 
        // Print CURL result. 
        echo $ch_result; 
    ?> 
    
    +0

    如果我使用這個curl_init('http://'。$ _SERVER ['SERVER_NAME']。$ _SERVER ['REQUEST_URI']);並運行它只是從未完成加載的腳本,但如果我將其更改爲$ ch = curl_init();它運行,但我沒有得到任何迴應 – 2013-03-23 14:37:29

    +0

    使用curl_init,它會將CURLOPT_URL設置爲傳遞給函數的值。因此,對於您嘗試,http://sample.sample.com/api – 2013-03-23 14:40:29

    0

    我想是這樣...

    $cmd = "wget -d --header=\"Content-Type:application/xml\" --post-data=\"$(cat <your xml file>)\" http://sample.sample.com/api"; 
    exec($cmd); 
    $outputfile = "dl.html"; 
    echo file_get_contents($outputfile); 
    
    +0

    有更多的系統高效的方法來做到這一點比炮轟。捲曲是正確的工具。 – 2013-03-23 14:19:07

    相關問題