2017-09-22 93 views
0
在简单的一步“XML URL数据

程序检索使用卷曲,得到了XML阵列 作为结果,则XML阵列以简单PHP数组,得到了如下的输出。阵列在foreach循环“条目”指数

Array ([channel] => SimpleXMLElement Object ([title] => Mobile01 本站新聞 
[link] => (link##) [description] => Mobile01 本站新聞 [pubDate] => Fri, 22 
Sep 2017 09:58:02 +0800 [item] => Array ([0] => SimpleXMLElement Object ( 
[title] => HP Envy 13-ad050TX 輕薄、獨顯、長效續航力 [link] => (link##) 
[pubDate] => Fri, 22 Sep 2017 09:52:41 +0800 [description] => 
SimpleXMLElement Object () [category] => HP) [1] => SimpleXMLElement 
    Object ([title] => 雙倍震撼 RealShow TWIN 高解析雙體藍牙喇叭 [link] => 
(link##) [pubDate] => Fri, 22 Sep 2017 09:43:45 +0800 [description] => 
SimpleXMLElement Object () [category] => 行動影音) 

现在我想仅从数组“项目”不通道..和当我试图foreach循环我得到信道下面的代码使用给定的不是“项目”。

$post=""; 
    $agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; 
    rv:1.8.1.1) 
    Gecko/20061204 Firefox/2.0.0.1'; 
    $url= "https://www.mobile01.com/rss/news.xml"; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt($ch, CURLOPT_URL, $url);   
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: 
    application/json')); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POST, 1);   
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 

    $result = curl_exec($ch); 
    curl_close($ch); 
    $xml = new SimpleXMLElement($result); 

    $array = array(); 
    foreach($xml as $k => $v) { 
    $array[$k] = $v; 
    } 

     $i=0; 
    print_r($array); 
    foreach ($array->item as $key=>$entry) { 

    echo "title===>". $title =$entry['title']; 
    echo "link===>".$link =$entry['link']; 

    echo "title111===>".$title11 = $entry -> title; 
    echo "description===>".$description = $entry -> description; 
尝试这种代码用不同的方面,当

遇到错误,

在简单的步骤3210
Fatal error: 
     require_once(): Failed opening required 'lib/class.XmlToArray.php' 
     (include_path='.:/usr/lib/php:/usr/local/lib/php') 

过程“XML URL数据检索使用卷曲,得到了XML阵列 作为结果,则XML阵列以简单PHP数组,得到了如在简单的一步below.procedure输出” XML URL数据检索使用卷曲,得到了XML数组 作为结果,然后将XML数组转换为简单的PHP数组,得到如下输出。

回答

0

您不需要包含任何其他库,只是坚持使用SimpleXML。一旦你得到正确的数据回来,你可以像处理它...

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

$post=""; 
$agent = 'spider'; 
$url= "https://www.mobile01.com/rss/news.xml"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($ch, CURLOPT_HEADER, false); 

$result = curl_exec($ch); 
curl_close($ch); 
$xml = new SimpleXMLElement($result); 

foreach ($xml->channel->item as $key=>$entry) {  
    echo "title=>".$entry -> title.PHP_EOL; 
    echo "description=>".$entry -> description.PHP_EOL; 
}