[英]Send an XML post request to a web server with CURL
我正在嘗試使用php和curl向Web服務器發送請求。 我之前沒有做過類似的事情,雖然網上有很多不錯的例子,但我對理解一些curl命令有些困難。
這就是我想要做的:有一個已建立的Web服務(例如:Web地圖服務),我希望我的php代碼向此服務發送一個發布XML請求。 作為回應,我想獲得一個XML文件。
這就是我現在所擁有的:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '');
/*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);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
/*curl_setopt($ch, CURLOPT_REFERER, '');*/
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ch_result = curl_exec($ch);
curl_close($ch);
echo $ch_result;
正如我所說,我在php中也很新,也使用curl,我想我缺少一些概念。 我的問題是:1)我必須放入的字符串(鏈接)是什么:
curl_setopt($ch, CURLOPT_URL, '');
它是我要發送請求的服務的主機名嗎?
2)在第6行中,變量$ xml包含我要作為請求發送的xml文件。 它是正確的還是這個變量應該包含其他內容?
3)在哪些情況下我需要使用httpheader或header(row3和row4);
謝謝您的幫助。 季米特里斯
試試這種方式:
$url = 'https://android.googleapis.com/gcm/send';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, "<xml>here</xml>" );
$result = curl_exec($ch);
curl_close($ch);
有關更多詳細信息,請訪問: http : //php.net/manual/en/function.curl-setopt.php
我認為使用HTTP類可能更適合發出HTTP請求。
見http://www.php.net/manual/intro.http.php 。
此外,還有適用於PHP的特定WMS庫,例如http://docs.huihoo.com/geoserver/1.6.0/Parsing%20and%20using%20WMS%20capabilities%20with%20PHP.html 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.