• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

我想用php读取xml的三个值

php 搞代码 3年前 (2022-01-23) 17次浏览 已收录 0个评论
文章目录[隐藏]
$url="http://www.cailele.com/static/ssc/newlyopenlist.xml";		$doc = new DOMDocument();		$doc->load($url); //读取xml文件		$lst = $doc->getElementsByTagName('row');		for ($i=0; $iitem($i);			$t1=$iframe->attributes->getNamedItem('expect')->value;			$t2=$iframe->attributes->getNamedItem('opencode')->value;			$t3=$iframe->attributes->getNamedItem('opentime')->value;			$t1=substr($t1,-9);			kjdata($t2,1,$t1,$t3);		 } 		 

我想读取xml的expec,opencode,opentime三个值,不知道为什么读取不上,求解答

回复讨论(解决方案)

$xml = <<xml;$doc = new DOMDocument();$doc->loadXML($xml); //读取xml文件$items = $doc->getElementsByTagName('row');for ( $i  =  0 ;  $i   length ;  $i ++) {	$t1 = $items -> item ( $i )->getAttribute('expect');//expect的值    $t2 = $items -> item ( $i )->getAttribute('opencode');//opencode的值    $t3 = $items -> item ( $i )->getAttribute('opentime');//opentime的值    var_dump($t1.'|'.$t2.'|'.$t3);}

经测试,你的代码没有问题
问题在于 $doc->load($url); 取不到值
该 url 用 file_get_contents 也取不到值
只有用 curl 才可取到

取到后可用 $doc->loadXML($str) 载入
然后就可用你的代码处理了

经测试,你的代码没有问题
问题在于 $doc->load($url); 取不到值
该 url 用 file_get_contents 也取不到值
只有用 curl 才可取到

取到后可用 $doc->loadXML($str) 载入
然后就可用你的代码处理了

能不能帮我发一下改了以后的代码,万分感谢

$url = "http://www.cailele.com/static/ssc/newlyopenlist.xml";$doc = new DOMDocument();$doc->loadXML(curl_get($url));$lst = $doc->getElementsByTagName('row');for ($i=0; $iitem($i);  $t1=$iframe->attributes->getNamedItem('expect')->value;  $t2=$iframe->attributes->getNamedItem('opencode')->value;  $t3=$iframe->attributes->getNamedItem('opentime')->value;  $t1=substr($t1,-9);  kjdata($t2,1,$t1,$t3);}function kjdata() {  echo join(', ', func_get_args()), PHP_EOL;}function curl_get($durl) {  $cookiejar = realpath('cookie.txt');  $t = parse_url($durl);  $ch = curl_init();  curl_setopt($ch, CURLOPT_URL,$durl);  curl_setopt($ch, CURLOPT_TIMEOUT,5);  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  curl_setopt($ch, CURLOPT_REFERER, "http://$t[host]/");  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  $r = curl_exec($ch);  curl_close($ch);  return $r;}

5,9,6,4,2, 1, 140421023, 2014-04-21 01:55:30
7,6,4,2,4, 1, 140421022, 2014-04-21 01:50:30

不错不错不错不错

$url = "http://www.cailele.com/static/ssc/newlyopenlist.xml";$doc = new DOMDocument();$doc->loadXML(curl_get($url));$lst = $doc->getElementsByTagName('row');for ($i=0; $iitem($i);  $t1=$iframe->attributes->getNamedItem('expect')->value;  $t2=$iframe->attributes->getNamedItem('opencode')->value;  $t3=$iframe->attributes->getNamedItem('opentime')->value;  $t1=substr($t1,-9);  kjdata($t2,1,$t1,$t3);}function kjdata() {  echo join(', ', func_get_args()), PHP_EOL;}function curl_get($durl) {  $cookiejar = realpath('cookie.txt');  $t = parse_url($durl);  $ch = curl_init();  curl_setopt($ch, CURLOPT_URL,$durl);  curl_setopt($ch, CURLOPT_TIMEOUT,5);  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  curl_setopt($ch, CURLOPT_REFERER, "http://$t[host]/");  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  $r = curl_ex<em style="color:transparent">本文来源gao.dai.ma.com搞@代*码#网</em><a>搞代gaodaima码</a>ec($ch);  curl_close($ch);  return $r;}

5,9,6,4,2, 1, 140421023, 2014-04-21 01:55:30
7,6,4,2,4, 1, 140421022, 2014-04-21 01:50:30

非常感谢,分都给你了,我还有别的问题,想发帖没分了,可不可以给我点分

给了你 200 可用分


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:我想用php读取xml的三个值

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址