剥离XML标记的curl和htmlentities

时间:2012-05-20 04:38:53

标签: php xml curl xampp

所以我正在开发一个php脚本,它使用Curl和POST方法将信息传递给服务器,然后以XML的形式从该服务器上获取信息。然后我想获取这些信息并将其解析为用户可读的格式。

我使用curl获取信息并将其加载到变量($ result)中,然后我想将其加载到simpleXML中并存储在变量$ routeinfo中。但是,当我执行var_dump($ routeinfo)时,似乎没有存储任何数据。我已经确认通过回显它会在$ result中出现数据。

我还认为这可能是因为当我回显$ result时,浏览器中没有显示XML(但如果我查看源代码就会出现)。只是为了检查虽然我使用了htmlentities但是这会导致很多错误,因为<被解释为它的html等价物。无论如何,我已经达成了一个冲动,已经记录了几个小时实施不同的建议无济于事。我的代码如下,我将不胜感激任何帮助。

<?php 
//retrieve form data in a variable
$input = $_POST['stopNo'] ;

//declare other variables needed
$api_key = 'xxxkey' ;
$api_id = 12345 ;
$url = 'url to send data to' ; 
$querystring = "appID=". $api_id . "&apiKey=" . $api_key . "&stopNo" . $input ;
$data = urlencode($url) ; 


//curl using

$ch = curl_init() ; 

//set up curl

curl_setopt($ch, CURLOPT_URL, 'urlsendingdatato');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'appID='.urlencode($api_id).'&apiKey=' .urlencode($api_key) . '&stopNo=' .urlencode($input) );
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0 ) ; 
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8") ;
curl_setopt($ch, CURLOPT_BINARYTRANSFER , TRUE) ;


//execute and gather info
$result = curl_exec($ch) ; 

//close connection
curl_close($ch) ;

//format data 
//ent at the moment serves no purpose doesn't render the information correctly
$ent = htmlentities($result);
$routeinfo = new SimpleXMLElement($result) ;

//print data
echo "<br> $result <br>" ; 
var_dump($routeinfo) ;


?> 

1 个答案:

答案 0 :(得分:0)

似乎在创建simpleXML对象时出现问题。 因此,您可以尝试使用定义良好的XML文件创建新的simpleXMLElement对象。如果这有效则意味着获取的xml文件格式不正确。

$xml="<?xml version='1.0'?><book><author>steve</author><price>200</price><name>Its my life</name></book>";
$test= new SimpleXMLElement($xml);
var_dump($test);

这对我有用!

但通常如果存在xml解析问题,simpleXMLElement会抛出 error 。它是否在您的情况下返回任何错误?或者您可能已关闭错误报告(error_reporting(0);)。

相关问题