PHP接收XML
我有一个小的PHP脚本,正在侦听POST请求。我一直在期待xml。 通常我是发送xml请求的人。但今天我站在了接受的一边 我想这可能是一个简单的听$u帖子的例子,但我想我可能是错的——我什么都没有得到 下面是我的脚本,它等待任何xml:PHP接收XML,php,xml,curl,Php,Xml,Curl,我有一个小的PHP脚本,正在侦听POST请求。我一直在期待xml。 通常我是发送xml请求的人。但今天我站在了接受的一边 我想这可能是一个简单的听$u帖子的例子,但我想我可能是错的——我什么都没有得到 下面是我的脚本,它等待任何xml: <?php if(isset($_POST)) { mail("me@myemail.com","some title i want", print_r($_POST, true)); }else{ die("uh, what happe
<?php
if(isset($_POST)) {
mail("me@myemail.com","some title i want", print_r($_POST, true));
}else{
die("uh, what happened?");
}
?>
下面是我从另一个地方发送的一个简单xml字符串:
<?php
$xml_data ='
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don\'t forget me this weekend!</body>
</note>
';
function sendXML2Server($URL,$XML){
$xml_data = trim($XML);
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
echo sendXML2Server('https://someurl.com/inboundxml.php',$xml_data)
?>
您只发送数据,这就是为什么PHP不能将此数据解释为某个键和值。因此,您需要将其作为变量值发送:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml_data' => $xml_data));
或作为原始post数据接收:
<?php
if(isset($HTTP_RAW_POST_DATA)) {
mail("me@myemail.com","some title i want", print_r($HTTP_RAW_POST_DATA, true));
}else{
die("uh, what happened?");
}
?>
CURLOPT\u POSTFIELDS需要一个数组:
curl\u setopt($ch,CURLOPT\u POSTFIELDS,array('content'=>$xml\u data))代码>
然后像这样检索它:
<?php
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['content'])) {
mail("me@myemail.com","some title i want", print_r($_POST['content'], true));
}else{
die("uh, what happened?");
}
?>
CURLOPT_POSTFIELDS应该是一个关联数组。您将其设置为字符串,然后尝试将其解析为数组。在本例中,是否不需要CURLOPT_HTTPHEADER?