如何使用cURL在PHP中执行RAW POST?
没有任何编码的原始帖子,我的数据存储在一个字符串中.数据应格式如下:
... usual HTTP header ...
Content-Length: 1039
Content-Type: text/plain
89c5fdataasdhf kajshfd akjshfksa hfdkjsa falkjshfsa
ajshd fkjsahfd lkjsahflksahfdlkashfhsadkjfsalhfd
ajshdfhsafiahfiuwhflsf this is just data from a string
more data kjahfdhsakjfhsalkjfdhalksfd
Run Code Online (Sandbox Code Playgroud)
一种选择是手动编写正在发送的整个HTTP标头,但这似乎不太理想.
无论如何,我可以只将选项传递给curl_setopt(),说使用POST,使用text/plain,并从一个$variable?发送原始数据?
我编写了一个类/函数,通过PHP4/cURL通过https发送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); …Run Code Online (Sandbox Code Playgroud)