我想在PHP中測試cURL選項。但是我無法在瀏覽器上打印XML輸出。我得到解析錯誤。出於某種原因,XML的開頭被截斷,因此語法錯誤。 我的客戶端代碼如下:無法使用cURL輸出xml PHP
<?php
header('Content-type: text/xml');
/**
* Define POST URL and also payload
*/
$xml_data = "<?xml version='1.0'?><member><name>XYZ</name><address>1111 yonge street Toronto Canada</address><businessunit>Hotel </businessunit></member>";
define('XML_PAYLOAD',$xml_data);
define('XML_POST_URL', 'http://localhost:8888/PlatformX/build_xml.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
/**
* Execute the request and also time the transaction
*/
$start = array_sum(explode(' ', microtime()));
$retValue = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;
/**
* Check for errors
*/
if (curl_errno($ch)) {
$result = 'ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 404:
$result = 'ERROR -> 404 Not Found';
break;
default:
$result = 'Success';
break;
}
}
/**
* Close the handle
*/
curl_close($ch);
/**
* Output the results and time
*/
echo 'Total time for request: ' . $totalTime . "\n";
echo $result;
print_r($retValue);
/**
* Exit the script
*/
exit(0);
?>
而且我塞雷爾語端代碼:
<?php
header('Content-type: text/xml');
foreach($_POST as $xmlstr) {
echo $xmlstr;
}
?>
但我無法打印在瀏覽器的XML。
請求你的幫助。 我得到響應
'1.0'?>XYZ
1111 yonge street Toronto Canada
Hotel
下,我沒有得到的標籤名,如果我用header('Content-type: text/xml');
我得到解析錯誤。
我改變內容類型爲文本/無格式...現在即時得到「 '1.0' 編碼= 'UTF-8' ?> XYZ 1111央街加拿大多倫多酒店 businessunit> 「出於某種原因,我沒有看到<?XML版本=越來越displayed.It只顯示‘1.0’ –
Nanda
開始那你改變你的服務器端代碼' echo file_get_contents(「php:// input」);'而不是'foreach'循環?我相應地改變了我的答案。 – akirk
哇,工作!非常感謝。 – Nanda