PHP4:通过HTTPS / POST通过cURL发送XML?
问题描述:
我写了一个类/函数通过PHP通过PHP4 / cURL发送xml,只是想知道这是否正确的方法,或者如果有一个更好的一个。
I wrote a class/function to send xml over https via PHP4/cURL, just wondering if this is the correct approach, or if there's a better one.
注意,PHP5目前不是一个选项。
Note that PHP5 is not an option at present.
/**
* 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;
}
干杯!
答
很好的解决方案!在这里也找到了类似的:
Great solution! Found a similar one here also:
- Post JSON/XML data with curl and receive it on other end
他们还展示了如何在服务器上接收这种XML / JSON
Also they have showed how to receive this kind of XML/JSON on server
// here you can have all the required business checks
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
$postText = file_get_contents('php://input');
}