dongtan6543
2015-11-10 08:36 阅读 112
已采纳

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 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.

    点赞 评论 复制链接分享
  • douwo1862 douwo1862 2015-11-10 08:47

    The url you are loading is returning a web-page: html.

    So when you treat it as a json string in:

    $content = json_decode($content, true);
    

    You will set your $content to null as that is what is returned when json_decode() cannot decode the string / the input is not valid json.

    If they have an api that returns json, you could use that, otherwise you can leave out the json_decode line and take it from there.

    点赞 评论 复制链接分享
  • dongxuan2015 dongxuan2015 2015-11-10 08:50

    If you tried outputting the $content variable right after doing the cURL, you would notice that the website throws a humongous error message.

    The error is basically some user-agent check that the website expects which you do not provide.

    If you insert this before you do your curl_exec(), you will get the content correctly:

    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');
    

    With that said, you will still get nothing because you're trying to decode JSON while the website does not return you a JSON string. This needs to be removed:

    $content = json_decode($content, true);
    

    Now everything should work as you want it to.

    点赞 评论 复制链接分享

相关推荐