PHP和CURL-POST-XML

PHP和CURL-POST-XML,php,xml,curl,post,Php,Xml,Curl,Post,我花了两天的时间想弄明白这一点,不知你们是否能帮忙?我试图向服务器发送一个XML字符串,它们声明“所有XML请求都应该使用HTTP post方法,使用“XML”参数名发送到我们的服务器”。我已经尝试了很多方法来实现这一点,但没有乐趣(如果我将javascript与表单一起使用,它可以很好地工作,但在项目中并不实用)。请有人给我指一下正确的方向好吗?我将在下面首先发布我无法使用的PHP/CURL代码,然后发布工作正常的Javascript代码。我想我只是想在PHP/CURL代码中模拟Javascr

我花了两天的时间想弄明白这一点,不知你们是否能帮忙?我试图向服务器发送一个XML字符串,它们声明“所有XML请求都应该使用HTTP post方法,使用“XML”参数名发送到我们的服务器”。我已经尝试了很多方法来实现这一点,但没有乐趣(如果我将javascript与表单一起使用,它可以很好地工作,但在项目中并不实用)。请有人给我指一下正确的方向好吗?我将在下面首先发布我无法使用的PHP/CURL代码,然后发布工作正常的Javascript代码。我想我只是想在PHP/CURL代码中模拟Javascript

PHP/CURL代码

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<Request>
   <Head>
      <Username>username</Username>
      <Password>password</Password>
      <RequestType>GetCities</RequestType>
   </Head>
   <Body>
      <CountryCode>MX</CountryCode>
  </Body>
</Request>';

$url = "http://example.com";

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;

curl_close($ch);
$xml='1!'
用户名
密码
GetCities
MX
';
$url=”http://example.com";
$ch=curl\u init($url);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:application/x-www-form-urlencoded');
curl_setopt($ch,CURLOPT_POSTFIELDS,“$xml”);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl\u exec($ch);
echo$输出;
卷曲关闭($ch);
Javascript代码(有效)

函数submitForm(){
var reqXML=“用户名密码GetCitiesMX”;
document.getElementById(“xml请求”).value=reqXML;
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
document.getElementById(“xml响应”).value=xmlhttp.responseText;
}
}
open(“POST”http://example.com“,对);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
send(“xml=“+reqXML”);
返回false;

当您希望通过“XML”参数发送XML内容时,您可能需要执行以下操作:

curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=" . $xml);
编辑:这是我的工作代码:

<?php

$xml = '<?xml version="1.0" encoding="UTF-8"?><Request><Head><Username>username</Username> <Password>password</Password><RequestType>GetCities</RequestType></Head><Body><CountryCode>MX</CountryCode></Body></Request>';

$url = "https://postman-echo.com/post"; // URL to make some test
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=" . $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);
echo '<pre>';
echo htmlentities($data);
echo '</pre>';

if(curl_errno($ch))
    print curl_error($ch);
else
    curl_close($ch);

?>


谢谢。我刚试过,结果不是出现通常的错误,而是出现了“不存在的IP地址”很抱歉,这可能与您尝试将请求发送到的主机或网络有关。您好。您的意思是什么?javascript方法工作正常,因此他们显然希望收到POST请求。不存在的IP地址问题现在已经消失!返回到“使用POST方法发送'xml'参数”由于错误我编辑了我的答案,您可以在您这边尝试我的代码,您会注意到POST参数“xml”包含您的xml数据。谢谢。我将在早上首先尝试一下
<?php

$xml = '<?xml version="1.0" encoding="UTF-8"?><Request><Head><Username>username</Username> <Password>password</Password><RequestType>GetCities</RequestType></Head><Body><CountryCode>MX</CountryCode></Body></Request>';

$url = "https://postman-echo.com/post"; // URL to make some test
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=" . $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$data = curl_exec($ch);
echo '<pre>';
echo htmlentities($data);
echo '</pre>';

if(curl_errno($ch))
    print curl_error($ch);
else
    curl_close($ch);

?>