【问题标题】:XML response from asp page来自 asp 页面的 XML 响应
【发布时间】:2011-12-07 05:26:33
【问题描述】:

我一直在尝试将 xml 消息从 php 发送到 asp,并使用 CURL 将响应输出到我的 php 页面,但没有收到任何响应。这是我尝试过的:

<?php
$url = "https://someweb.asp";
$post_string = "xmlmessage=<?xml version='1.0' encoding='UTF-8'?> 
<abc>
<UserId>123</UserId> 
</abc>";

//$header  = "POST HTTPS/1.0 \r\n";
$header = "Content-type: text/xml \r\n";
$header .= "Content-length: ".strlen($post_string)." \r\n";
$header .= "Content-transfer-encoding: text \r\n";
$header .= "Connection: close \r\n\r\n"; 
$header .= $post_string;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);

$output = curl_exec($ch);
$info = curl_getinfo($ch);

if ($output == false || $info['http_code'] != 200) {
  $output = "No cURL data returned for $url [". $info['http_code']. "]";
  if (curl_error($ch))
    $output .= "\n". curl_error($ch);
  }
else
    {curl_close($ch);}

echo $output;
?>

谁能指导我哪里错了?

【问题讨论】:

    标签: php xml ajax curl asp-classic


    【解决方案1】:

    不要为简单的 POST 构建自定义请求。 CURL 完全有能力在没有所有这些恶作剧的情况下发帖:

    $xml = <<<EOL
    <?xml version='1.0' encoding='UTF-8'?> 
    <abc>
    <UserId>123</UserId> 
    </abc>
    EOL;
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('xmlmessage' => $xml));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die(curl_error($ch));
    }
    
    echo $result
    

    【讨论】:

    • 我在尝试您的代码时得到了这个:警告:curl_setopt() [function.curl-setopt]:第 25 行的 /home/add.php 中的无效 curl 配置选项错误请求(无效编号)
    • 第 25 行:curl_setopt($ch, CURLOPT_POST_FIELDS, array('xmlmessage' => $xml));
    • 对不起,我的错字。应该是 CURLOPT_POSTFIELDS(只有一个_
    • 太棒了。 javascript呢?函数 loadXMLDoc() { 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("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("POST","someweb.asp",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("123"); }
    • 如果该 .asp 页面位于您从中加载此页面/脚本的服务器以外的服务器上,则无法执行此操作。查找 JS/ajax 的“同源”安全策略。
    【解决方案2】:
    $post_string = "xmlmessage=<?xml version='1.0' encoding='UTF-8'?> 
    <abc>
    <UserId>123</UserId> 
    </abc>";
    

    交换双引号和单引号

    $post_string = 'xmlmessage=<?xml version="1.0" encoding="UTF-8"?> 
    <abc>
    <UserId>123</UserId> 
    </abc>';
    

    单引号在 xml 标记中无效

    【讨论】:

    • 那没有帮助。当我这样做时出现错误:“~No xmlmessage= parameter was provided”:
    猜你喜欢
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2011-01-27
    • 1970-01-01
    相关资源
    最近更新 更多