【问题标题】:file_get_html() not working in Google App Enginefile_get_html() 在 Google App Engine 中不起作用
【发布时间】:2017-08-24 08:58:02
【问题描述】:

我有一些 PHP 代码在本地和 Web 中都能完美运行(使用非 SSL 托管)。我使用file_get_html(url) 获取文件内容,一切正常。

当我尝试在 Google App Engine 中运行此代码时出现问题。 file_get_html() 函数不起作用。

以下代码在 BEGINEND 之间没有显示任何内容:

<?php
header("Access-Control-Allow-Origin: *");

include './simple_html_dom.php';

$sitioweb = file_get_html("http://www.bolsamadrid.es/esp/aspx/Mercados/Precios.aspx?indice=ESI100000000");

echo "BEGIN<hr>";
echo $sitioweb;
echo "<hr>END";

任何帮助将不胜感激!

【问题讨论】:

  • 当我在这个网站上查看 simplehtmldom.sourceforge.net 他们给出这个代码来转储 html "file_get_html('foo')->plaintext;"所以也许 $sitioweb->plaintext 会起作用?
  • 在本地工作,但不能在 Google App Engine 中工作。问题不在于 simple_html_dom 库,我有很多员工在处理它;问题是我无法在 App Engine 中使用它,因为我无法获取外部网络的内容。
  • 如果你只做一个 file_get_content 会发生什么?
  • file_get_content 在应用程序中也不起作用 :-(
  • 那是google app engine的设置问题,你没有报错吧?

标签: php google-app-engine google-cloud-platform


【解决方案1】:

我终于找到了解决方案。我用curl 代替file_get_html

在 Google App Engine 中不适合我的代码:

$sitioweb = file_get_html("http://www.bolsamadrid.es/esp/aspx/Mercados/Precios.aspx?indice=ESI100000000");

在 Google App Engine 中非常适合我的代码:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.bolsamadrid.es/esp/aspx/Mercados/Precios.aspx?indice=ESI100000000');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
$str = curl_exec($curl);
curl_close($curl);

$sitioweb = str_get_html($str);

它有点冗长但有效:-)

【讨论】:

    【解决方案2】:

    必须安装 PHP cURL 才能使其工作:

    sudo apt-get install curl
    

    然后重启apache服务器。

    【讨论】: