基于响应的xml - PHP

时间:2011-09-30 15:38:49

标签: php xml

我向服务器提交了一个值......就像这样

abc.php

<?php
$input = "some value"
//Some functions

上面会产生一些独特的回应,比如111

现在我需要根据这样的响应上传一个xml

$xml='<?xml version= "1.0 .....>
<id>111</id>(this id should be dynamic and based on the response)'
?>

这个xml在上面的php文件(abc.php)

我该怎么做?

2 个答案:

答案 0 :(得分:0)

如果你有一个脚本 abc.php

<?php

// Gets a value from a URL GET parameter.
$value = $_GET[ 'value' ];

echo $value;

然后你可以像第二个脚本 upload.php

<?php

// Retrieves the response from /abc.php
$urlResponse = file_get_contents( 'http://localhost/abc.php?value=foobar' );

header( 'Content-type: text/xml; charset=utf-8' );

echo '<?xml version="1.0" encoding="utf-8" ?>', PHP_EOL;
echo '<doc>', PHP_EOL;
echo '<id>', $urlResponse, '</id>', PHP_EOL;
echo '</doc>', PHP_EOL;

脚本upload.php 检索abc脚本的响应(使用file_get_contents函数),生成XML输出

更多信息:http://www.php.net/file_get_contents

答案 1 :(得分:0)

根据您的评论,这可能对您有所帮助

备选方案A :从abc.php脚本中写一个XML文件

<?php

// Gets a value from a URL GET parameter.
$value = $_GET[ 'value' ];

$xml = <<<XML
<?xml version="1.0" encoding="utf-8" ?>
<doc>
    <id>$value</id>
</doc>
XML;

// The location of the XML that is about to be generated.
$xmlPath = 'output.xml';
if ( file_put_contents( $xmlPath, $xml ) === false )
{
    trigger_error( 'There was an error trying to write the XML file (check permissions)', E_USER_NOTICE );
}

备选方案B :使用abc.php从脚本upload.php输出XML响应

abc.php

<?php
function doSomethingHere()
{
    return rand();
}

upload.php的

<?php
// Include your PHP library.
require 'abc.php';
// Set the correct Content type to instruct browsers to treat the document as XML file.
header( 'Content-type: text/xml; charset=utf-8' );
?>
<?xml version="1.0" encoding="utf-8" ?>
<doc>
    <id><?php echo doSomethingHere(); ?></id>
</doc>