使用file_get_contents()接收XML

时间:2012-02-13 13:53:19

标签: php xml web-services file-get-contents

我是这个网站的新手,也是网络服务的新手。我正在尝试创建一个Web服务,将xml响应发送回客户端请求。我遇到的问题是当我执行客户端请求时,浏览器挂起并最终返回服务器超时错误。我检查了PHP.INI设置以确保allow_url_fopen设置为on。 我在使用Zend Server的IBM i上托管该网站。 在此先感谢您的帮助。

这就是我尝试这样做的方式:

服务器端:此文件名为XML3.php

<?php
ob_start();
header("Content-Type:text/xml");
echo "<?xml version='1.0' encoding='UTF-8' ?>";
echo '<posts>';
    echo '<post>';
    echo "<title>title1</title>";
    echo "<body>this is the body</body>";
    echo "<post_date>02/10/2012</post_date>";
    echo '</post>';
echo '</posts>';
ob_end_flush();

?>

客户方:

<?php 
$xml = file_get_contents('http://corvetteamericadealers.com/dev1/Examples/XML3.php');
$sxe = new SimpleXMLElement($xml);
var_dump($sxe); 

2 个答案:

答案 0 :(得分:2)

这段代码对我来说很好。请检查。

<?php
function curl_get_file_contents($URL)
{
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $URL);
        $contents = curl_exec($c);
        curl_close($c);

        if ($contents) return $contents;
            else return FALSE;
 }

$xmlString = curl_get_file_contents("http://corvetteamericadealers.com/dev1/Examples/XML3.php");
$xml = simplexml_load_string($xmlString);
var_dump($xml); 
?>

这是我的输出。

object(SimpleXMLElement)#1 (1) 
{ ["post"]=> object(SimpleXMLElement)#2 (3) 
{ 
["title"]=> string(6) "title1" 
["body"]=> string(16) "this is the body" 
["post_date"]=> string(10) "02/10/2012" } 
}

答案 1 :(得分:0)

您是通过解析soap xml来实现Web服务的吗?使用一些Web服务机制从.wsdl文件生成类。

相关问题