通过 PHP 远程 url 发送 XML 文件 [英] Sending XML file through PHP remote url

查看:37
本文介绍了通过 PHP 远程 url 发送 XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如下.我必须将定义的 XML 发送到 url http:\\www.example.com:1234 ,其中包含一些我之前必须定义的变量.

The situation is as follows. I have to send a defined XML to an url http:\\www.example.com:1234 with some variables that I have to previously define.

XML 是这样的:

<Title1>
<Title2>Some Text</Title2>
<Title3>Variable 1</Title3>
<Title4>Some Text</Title4>
<Title5>
    <Title51>Variable 2</Title51>
</Title5>
</Title1>

但是,我想在 html/php 表单和 get 方法中定义这些变量 (1, 2),这样用户就可以引入这两个变量,然后单击表单中的提交按钮将 XML 发送到以前的网址.

But, I want to define those variables (1, 2) within an html/php form and get method, so the user can introduce both variables and then click on the submit button from the form to send the XML to the previously URL.

此外,XML 应该具有 "Content-Type","application/x-www-form-urlencoded" 标头.

Also, XML should have the "Content-Type","application/x-www-form-urlencoded" header.

这可能吗?我已经尝试将这些变量直接传递给 XML,我所想到的最好的方法是显示 XML 而不是解析 php 字符串.

Is this possible? I've tried to pass these variables directly to the XML and the best that I've come to is to showing the XML and not parsing the php strings.

此外,我尝试了一些脚本,例如 PHP 类中的 simplexml,但到目前为止都没有成功.

Also, I've tried some scripts like simplexml from PHP classes, but with no luck so far.

推荐答案

1) 使用新值修改现有 XML.试试这个

1) To modify existing XML with new values . Try this

sample.xml:

<Title1>
   <Title2>Some Text</Title2>
   <Title3>Variable 1</Title3>
   <Title4>Some Text</Title4>
   <Title5>
      <Title51>Variable 2</Title51>
   </Title5>
</Title1>

PHP:

$xml = simplexml_load_file("sample.xml");
$xml->Title3 = $_GET['t3'];              // Updating <Title3></Title3> from GET method
$xml->Title5[0]->Title51 = $_GET['t5'];  // Updating <Title51></Title51> from GET method
$xml->asXML('sample.xml');               // saving the xml file

<小时>

2)创建新的 XML 文件 (sample.xml) :


2)To create new XML file (sample.xml) :

PHP:

$xml = new SimpleXMLElement("<Title1></Title1>");
$xml->Title2='Some Text';
$xml->Title3 = $_GET['t3'];
$xml->Title4='Some Text';
$xml->Title5[0]->Title51 = $_GET['t5'];
$xml->asXML('sample.xml');               // saving the xml file

我已经向您展示了评论中提到的两种可能性.使用任何让你感到安慰的人:)

I have showed you both possibilities mentioned in the comment . Use anyone which comforts you :)

这篇关于通过 PHP 远程 url 发送 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆