【问题标题】:file_get_html(); not working with Teleduino linksfile_get_html();不使用 Teleduino 链接
【发布时间】:2016-04-27 17:32:26
【问题描述】:

我正在使用 Arduino 制作一个家庭自动化项目,我正在使用 Teleduino 远程控制 LED 作为测试。
我想把this link的内容显示到一个php页面中。

<!DOCTYPE html>
<html>
<body>

<?php

include 'simple_html_dom.php';

echo file_get_html('http://us01.proxy.teleduino.org/api/1.0/2560.php?k=202A57E66167ADBDC55A931D3144BE37&r=definePinMode&pin=7&mode=1');

?>

</body>

问题是函数没有返回任何东西。

我的代码有问题吗?
我可以使用任何其他功能向页面发送请求并获取该页面作为回报吗?

【问题讨论】:

  • error_reporting(E_ALL); ini_set('display_errors', '1');
  • 您的 URL 检索 HTML(在浏览器中测试)?我不这么认为。
  • @AbraCadaver 这会有什么用?
  • @fusion3k 我要从中检索数据的页面中没有 html,它只是一个不由标签分隔的文本字符串。会不会是这个问题?
  • 阅读 API 文档。它是 JSON,所以你必须使用 json_decode

标签: php html get arduino simple-html-dom


【解决方案1】:

我认为您必须使用函数 file_get_contents 但您的服务器正在保护数据不被抓取,因此 curl 将是一个更好的解决方案:

<?php 

// echo file_get_contents('http://us01.proxy.teleduino.org/api/1.0/2560php?k=202A57E66167ADBDC55A931D3144BE37&r=definePinMode&pin=7&mode=1');

                      // create curl resource
    $ch = curl_init();

    // set url
    curl_setopt($ch, CURLOPT_URL, "http://us01.proxy.teleduino.org/api/1.0/2560.php?k=202A57E66167ADBDC55A931D3144BE37&r=definePinMode&pin=7&mode=1");

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    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');

    // $output contains the output string
    $output = curl_exec($ch);

    echo $output;

    // close curl resource to free up system resources
    curl_close($ch);

?>

【讨论】:

  • 好的,我正在尝试,它正在工作。谢谢!如果你说它更好,我会考虑允许 file_get_content 函数工作
  • 我并不是说这样更好:) curl 非常适合,它模拟浏览器,因此您的网络服务器认为访问来自真正的浏览器:) 不客气
猜你喜欢
  • 2014-06-05
  • 2014-04-05
  • 2015-06-21
  • 2013-02-04
  • 2019-09-10
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多