【问题标题】:PHP and CURL POST XMLPHP 和 CURL POST XML
【发布时间】:2018-06-13 08:16:54
【问题描述】:

我已经把头发扯了 2 天,试图解决这个问题,我想知道你们中的任何人是否可以提供帮助?我正在尝试将 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);

Javacript 代码(有效)

function submitForm() {
var reqXML = "<Request><Head><Username>username</Username> <Password>password</Password><RequestType>GetCities</RequestType></Head><Body><CountryCode>MX</CountryCode></Body></Request>";
document.getElementById("xml-request").value = reqXML;
var xmlhttp;
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
} else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("xml-response").value = xmlhttp.responseText;
    }
}
xmlhttp.open("POST", "http://example.com", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("xml=" + reqXML);
return false;

【问题讨论】:

    标签: php xml curl post


    【解决方案1】:

    当您想通过“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 数据。
    • 谢谢。我会在早上第一件事去尝试
    猜你喜欢
    • 2018-06-13
    • 2016-05-17
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多