[英]PHP Return XML with stream_context_create()
I'm using stream_context_create() instead of cURL to make an HTTP post. 我正在使用stream_context_create()而不是cURL来进行HTTP发布。 It works, but file_get_contents() returns a single string of all the data instead of an XML object.
它可以工作,但是file_get_contents()返回所有数据的单个字符串,而不是XML对象。 How can I modify my code so it returns an XML object and I can access the values individually?
如何修改代码,使其返回XML对象,并且可以单独访问这些值?
<?php
$url = 'http://www.domain.com';
$data = array( 'name1' => 'data1', 'name2' => 'data2', 'name3' => 'data3' );
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query( $data )
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
var_dump($result); // data1data2data3
?>
And the page that returns the XML: 返回XML的页面:
<?php
header('Content-type: text/xml');
$xml = '<result><name1>' . $_POST['data1'] . '</name1><name2>' . $_POST['data2'] . '</data2><name3>' . $_POST['data3'] . '</name3></result>';
echo $xml;
?>
I think that you're looking at this var_dump in your browser, and browser tries to render tags <result>
and <name..>
as standard HTML. 我认为您正在浏览器中查看此var_dump,浏览器尝试将标签
<result>
和<name..>
呈现为标准HTML。
Please try to run this code, and check the source of your webpage (in Chrome, you can do it by prepending page URL by view-source:
) 请尝试运行此代码,并检查网页的来源(在Chrome浏览器中,您可以通过在页面URL前面添加
view-source:
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.