我想用php读取xml的三个值

快跑稻草人 2014-04-20 10:25:02
$url="http://www.cailele.com/static/ssc/newlyopenlist.xml";
$doc = new DOMDocument();
$doc->load($url); //读取xml文件
$lst = $doc->getElementsByTagName('row');
for ($i=0; $i<2; $i++) {
$iframe= $lst->item($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三个值,不知道为什么读取不上,求解答
...全文
276 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
快跑稻草人 2015-09-27
  • 打赏
  • 举报
回复
引用 4 楼 xuzuning 的回复:
$url = "http://www.cailele.com/static/ssc/newlyopenlist.xml";
$doc = new DOMDocument();
$doc->loadXML(curl_get($url));
$lst = $doc->getElementsByTagName('row');
for ($i=0; $i<2; $i++) {
  $iframe= $lst->item($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
你好,现在又获取不到了,您试试这段代码,返回错误的
hackol 2014-04-25
  • 打赏
  • 举报
回复
$url = "http://www.cailele.com/static/ssc/newlyopenlist.xml"; $doc = new DOMDocument(); $doc->loadXML(curl_get($url)); $lst = $doc->getElementsByTagName('row'); for ($i=0; $i<2; $i++) { $iframe= $lst->item($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; } 这样也还是读不到。
hackol 2014-04-25
  • 打赏
  • 举报
回复
读不到是为啥呢?
快跑稻草人 2014-04-24
  • 打赏
  • 举报
回复
引用 7 楼 xuzuning 的回复:
给了你 200 可用分
你有3W多分,才给我200
yf20051215 2014-04-24
  • 打赏
  • 举报
回复
4楼 这位大神 你改完这个代码测试可以用吗 我试了怎么不行呢 无法显示网页
yf20051215 2014-04-24
  • 打赏
  • 举报
回复
楼上的朋友,这个代码你能正常用吗
xuzuning 2014-04-22
  • 打赏
  • 举报
回复
给了你 200 可用分
快跑稻草人 2014-04-22
  • 打赏
  • 举报
回复
引用 4 楼 xuzuning 的回复:
$url = "http://www.cailele.com/static/ssc/newlyopenlist.xml";
$doc = new DOMDocument();
$doc->loadXML(curl_get($url));
$lst = $doc->getElementsByTagName('row');
for ($i=0; $i<2; $i++) {
  $iframe= $lst->item($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
非常感谢,分都给你了,我还有别的问题,想发帖没分了,可不可以给我点分
嘻哈大咖秀 2014-04-21
  • 打赏
  • 举报
回复
$xml = <<<xml
<?xml version="1.0" encoding="utf-8"?>
<xml><row expect="20140421005" opencode="5,7,3,1,5" opentime="2014-04-21 00:25:30" /><row expect="20140421004" opencode="9,4,7,4,5" opentime="2014-04-21 00:20:30" /><row expect="20140421003" opencode="3,4,4,3,9" opentime="2014-04-21 00:15:30" /><row expect="20140421002" opencode="7,0,2,9,4" opentime="2014-04-21 00:10:30" /><row expect="20140421001" opencode="6,3,8,3,1" opentime="2014-04-21 00:05:30" /><row expect="20140420120" opencode="6,9,3,5,3" opentime="2014-04-21 00:00:30" /><row expect="20140420119" opencode="7,7,0,7,2" opentime="2014-04-20 23:55:30" /><row expect="20140420118" opencode="8,9,4,6,9" opentime="2014-04-20 23:50:30" /><row expect="20140420117" opencode="1,1,2,4,0" opentime="2014-04-20 23:45:30" /><row expect="20140420116" opencode="4,3,8,1,0" opentime="2014-04-20 23:40:30" /></xml>
xml;
$doc = new DOMDocument();
$doc->loadXML($xml); //读取xml文件
$items = $doc->getElementsByTagName('row');
for ( $i = 0 ; $i < $items -> 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);
}

xuzuning 2014-04-21
  • 打赏
  • 举报
回复
$url = "http://www.cailele.com/static/ssc/newlyopenlist.xml";
$doc = new DOMDocument();
$doc->loadXML(curl_get($url));
$lst = $doc->getElementsByTagName('row');
for ($i=0; $i<2; $i++) {
  $iframe= $lst->item($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
快跑稻草人 2014-04-21
  • 打赏
  • 举报
回复
引用 2 楼 xuzuning 的回复:
经测试,你的代码没有问题 问题在于 $doc->load($url); 取不到值 该 url 用 file_get_contents 也取不到值 只有用 curl 才可取到 取到后可用 $doc->loadXML($str) 载入 然后就可用你的代码处理了
能不能帮我发一下改了以后的代码,万分感谢
xuzuning 2014-04-21
  • 打赏
  • 举报
回复
经测试,你的代码没有问题 问题在于 $doc->load($url); 取不到值 该 url 用 file_get_contents 也取不到值 只有用 curl 才可取到 取到后可用 $doc->loadXML($str) 载入 然后就可用你的代码处理了
yfgcq 2014-04-21
  • 打赏
  • 举报
回复
不错不错不错不错

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧