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

发布于 2024-11-07 15:30:57 字数 2359 浏览 0 评论 0原文

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

这是 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>

这是使用 cURL 的 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;
}
?>

任何帮助将不胜感激!

I have a datafile.xml that needs to be sent to a webservice.
I am using php curl to send the file. The problem is I don't know how to access and send the contents of the datafile.xml.

Here is content of the 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>

Here is my send php file -

<?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>

And here is the post_xml.php file that uses cURL -

<?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;
}
?>

Any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花落人断肠 2024-11-14 15:30:57

如果您不需要解析 XML 文件,那么我建议 fopen()file_get_contents()

为了简单起见,假设 datafile.xml 位于同一目录中,请尝试:

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

If you don't need to parse the XML file, then I'd suggest fopen() or file_get_contents().

For simplicity sake, assuming datafile.xml is in the same directory, try:

$xml = file_get_contents('datafile.xml');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文