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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题