我有一個web服務,我發送一個xml請求(application/x-www-form-urlencoded編碼)並獲得響應。 這些被髮送到包含名爲「XML」在PHP中使用Curl發送xml到webservices,但返回錯誤
當我使用一個簡單的HTML表單的查詢參數中的URL,如下面的一個,我返回的結果。但是,當我使用我的PHP代碼時,我返回了一個錯誤。也許這是因爲:這些被髮送到一個名爲'xml'的查詢參數中包含的URL?如果是這樣的話,我該如何發送該參數?如果有人能指出我做錯了什麼,我會非常感激。非常感謝
<form method="post" name="form1" action="http://webservicesapi.com/login.pl">
<textarea cols="80" rows="20" name="xml">
<?xml version="1.0"?><request><auth username="hello" password="world" /><method action="login" /></request>
</textarea>
<input type="submit" value="submit XML document">
</form>
這不起作用:
<?php
// open a http channel, transmit data and return received buffer
function xml_post($xml, $url, $port)
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off'))
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 15); // times out after 15s
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); // add POST fields
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
if($port==443)
{
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$xml = '<?xml version="1.0"?><request><auth username="hello" password="world" /><method action="login" /></request>';
$url ='http://webservicesapi.com/login.pl';
$port = 80;
$response = xml_post($xml, $url, $port);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<P><?=nl2br(htmlentities($response));?></P>
</body>
</html>
?>
你可能想發表您的錯誤。 –