php 读取xml文档

作者:袖梨 2022-06-24
 代码如下 复制代码

$xml = < < < xml 
  
    
  provision   
    
  1   
  01112233   
  00148888888888   
  www.111com.net   
  1   
  2   
  3   
 
  
 
  
xml; 
$x = simplexml_load_string($xml); 
//$x = simplexml_load_string(file_get_content(url)); 
print_r($x); 

out: 
simplexmlelement object 

  [@attributes] => array 
  ( 
  [version] => 1.5 
  ) 

  [command_name] => provision 
  [command_data_block] => simplexmlelement object 
  ( 
  [action_id] => 1 
  [service_id] => 01112233 
  [mid] => 00148888888888 
  [mobile_id] => 13999998888 
  [access_mode] => array 
  ( 
  [0] => 1 
  [1] => 2 
  [2] => 3 
  ) 

  ) 

   
   
       
       3   
       张三   
   
   
       
       5   
       李四   
   
   
  

$url="http://mb.111com.net/ws.php?q=content";  
    $dom = new domdocument();   
    $dom->load($url);  
    //print"

";   
    //print( $dom->savexml());  
    $dictarray = $dom->getelementsbytagname("dict");  
    foreach( $dictarray as $dict ) {  
          $key = $dict->getelementsbytagname("key");  
          $key=$key->item(0)->nodevalue;  
          $key= iconv("utf-8","gb2312",$key);     //注意要转码对于中文,因为xml默认为utf-8格式  
          echo ("***输出>key>的内容:****:".$key);  
 
          $lang = $dict->getelementsbytagname("lang");  
          $lang = utf8_decode($lang->item(0)->nodevalue);  
          echo "
";  
          echo ("***输出>lang>的内容:****:".$lang);  
     }  


方法三

$fp=fsockopen("xxx.xxx.com", 80, $errno, $errstr, 30);
 if($fp){
  $out = "get /abc.xml?key=value http/1.1 ";  
     $out .= "host: xxx.xxx.com ";  
     $out.= 'user_agent: xml-rpc client ';
     $out.='content-type: text/xml ';
     $out .= "connection: close ";    
    
      fwrite($fp,$out);
  while(!@feof($fp)){
         $result.=@fgets($fp,1024);
       }
       fclose($fp);  
 }
 echo $result;

相关文章

精彩推荐