如何使用PHP curl将xml文件内容发送到webservice

问题描述:

我有一个datafile.xml需要发送到web服务。 我使用php curl发送文件。问题是我不知道如何访问和发送datafile.xml的内容。如何使用PHP curl将xml文件内容发送到webservice

这里是datafile.xml的内容 -

<?xml version="1.0" encoding="ISO-8859-1"?> 
<inventoryUpdateRequest version="1.0"> 
<action name="bookupdate"> 
<username>user</username> 
<password>issecret</password> 
</action> 
<WebList> 
<Website> 
<transactionType>delete</transactionType> 
<vendorBookID>FaNuPh1</vendorBookID> 
</Website> 
</WebList> 
</inventoryUpdateRequest> 

这是我送的PHP文件 -

<?php require_once('post_xml.php');?> 
<?php 

$xml = HERE IS MY LACK OF UNDERSTANDING HOW TO EXTRACT datafile.xml CONTENT; 

$url ='https://inventoryupdate.website.com'; 
$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> 

这里是使用卷曲的post_xml.php文件 -

<?php 
// open a http channel, transmit data and return received buffer 
function xml_post($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, $post_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文件,那么我建议fopen()file_get_contents()

为了简单起见,假设datafile.xml是在同一目录下,请尝试:

$xml = file_get_contents('datafile.xml'); 
+0

这将是最好检索从我的桌面XML文件。我的导出xml文件的数据库程序只能在本地导出(不会导出到远程服务器)。 – Lily 2011-05-17 02:35:06