小编典典

ASP页面的XML响应

ajax

我一直在尝试从php向asp发送xml消息,并使用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;
?>

谁能指导我我做错了什么?


阅读 299

收藏
2020-07-26

共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
2020-07-26