2008-10-26 53 views
7

我寫了一個類/函數通過PHP4/cURL通過https發送xml,只是想知道這是否是正確的方法,或者是否有更好的方法。PHP4:通過cURL通過HTTPS/POST發送XML?

請注意,PHP5目前不是一個選項。

/** 
* Send XML via http(s) post 
* 
* curl --header "Content-Type: text/xml" --data "<?xml version="1.0"?>...." http://www.foo.com/ 
* 
*/ 
function sendXmlOverPost($url, $xml) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 

    // For xml, change the content-type. 
    curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); 

    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // ask for results to be returned 
    if(CurlHelper::checkHttpsURL($url)) { 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    } 

    // Send to remote and return data to caller. 
    $result = curl_exec($ch); 
    curl_close($ch); 
    return $result; 
} 

乾杯!

+0

哇,不是目前的選擇嗎? :(另外,如果你沒有設置cURL,看看HTTP_Request(http://pear.php.net/package/HTTP_Request) – Till 2008-10-27 03:21:52

回答

-1

使用提供與大多數PHP安裝

SoapClient

一個例子是:

$soap = new SoapClient("http://some.url/service/some.wsdl"); 
$args = array("someTypeName" => "someTypeValue" 
       "someOtherTypeName" => "someOtherTypeValue"); 

$response = $soap->executeSomeService($args); 

print_r($response); 
+0

乾杯,但我不認爲我使用的XML API實際上使用SOAP ,只是從帖子中讀取一個字符串。 – starmonkey 2008-10-26 23:45:53

3

$ ch = curl_init($ serviceUrl);

if($this -> usingHTTPS()) 
    { 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->sslVerifyHost); 

    } 

    curl_setopt($ch,CURLOPT_POST,TRUE); 
    curl_setopt($ch, CURLOPT_HEADER, FALSE);  
    curl_setopt ($ch, CURLOPT_POSTFIELDS, "OTA_request=".urlencode($this->xmlMessage)); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 

    $this->xmlResponse = curl_exec ($ch); 

    $this -> callerFactory -> dbgMsg('xmlResponse: <hr><pre>'.htmlentities($this->xmlResponse).'</pre><hr>'. curl_error($ch)); 
    curl_close ($ch); 

    $this->checkResponse(); 
4

偉大的解決方案!在這裏發現了類似的一個也:

此外,他們還展示瞭如何接受這種XML的/ JSON服務器上

// here you can have all the required business checks 
if ($_SERVER['REQUEST_METHOD'] === 'POST'){ 
    $postText = file_get_contents('php://input'); 
}