dongtan6543 2015-11-10 08:36
浏览 126
已采纳

PHP curl从网站返回空数组的数据

I am trying to write a Php Script to pull snow and other data from www.snowbird.com/mountain-report/ to display via an led array. I am having troubles with getting the data I need. I can't seem to be able to find a way to make it work. Would I be able to make this work, or would I have to go about and use a different language?

The following code only return empty. Following the code I will post what is returned.

<?php
require('simple_html_dom.php');

$ch = curl_init("http://www.snowbird.com/mountain-report/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);

$html = new simple_html_dom(); 
$html->load($content);

$ret1 = $html->find('.snowfall-total');
print_r ($ret1);
$ret2 = $html->find('#twenty-four-hour');
print_r ($ret2);
$ret3 = $html->find('#forty-eight-hour');
print_r ($ret3);
$ret4 = $html->find('#current-depth');
print_r ($ret4);
$ret5 = $html->find('#year-to-date');
print_r ($ret5);
?>

Here is the output

pi@KPi /var/www $php test4.php

Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
  • 写回答

3条回答 默认 最新

  • dpqg86714 2015-11-10 08:50
    关注

    The website you are trying to request from throws an error when using cURL because the google tools they have implemented in their python code crash when there is no user agent set.

    Try adding this line to your code before curl_exec($ch)

    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

    And as @jeroen said, using json_decode($content, true) is not neccesary as the returned data will be HTML code not a json string. Remove that line as well and you should be good to go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧