简单的HTML DOM - 无法打开流:连接超时

时间:2017-04-09 06:06:58

标签: php file-get-contents simple-html-dom

您好我将我的网站托管到一个免费托管网站(5gbfree.com),我在这里创建了一个函数,它从网站http://ppa.com.ph??q=fcer_view中检索比索美元汇率,只需选择{{1} } element并将值作为明文获取。它昨天正在工作,但是当我今天早上再次检查它时,它无法取出。因此它将返回默认值50。

error_log中:

a.active

我使用的是简单的html DOM。 这是功能。

[09-Apr-2017 13:49:32 Asia/Manila] PHP Warning: file_get_contents(http://www.ppa.com.ph/?q=fcer_view): failed to open stream:     Connection timed out in /home/rasibaseport/public_html/simple_html_dom2.php on line 75

不幸的是,5gbfree禁用了curl_init()函数。

include "simple_html_dom2.php";

function PPA_peso_dollar_rate(){
// Create DOM from URL or file
error_reporting(E_ALL);
ini_set("display_errors", 0);
ini_set('default_socket_timeout', 15);
$html = file_get_html("http://www.ppa.com.ph/?q=fcer_view");
$ret = 0;
if($html === false){
    $ret = 50;
}else {
    foreach($html->find('a[class=active]') as $e) 
    $ret = $e->plaintext;
    $explode = explode(" ", $ret);

    $ret = 50;
    foreach($explode as $ex){
        if(is_numeric($ex)){
            $ret = $ex;
        }
    }
    if($ret == 0) $ret = 50;
}

echo $ret;
}

这里有一轮工作吗?我感谢任何帮助。非常感谢你。

编辑:我忘了提到用localhost(xampp)测试这个,预期的返回值是正确的,没有错误和警告。工作得很好。

更新:在尝试@ Rafiq的更新解决方案后,没有任何效果。它给了我类似的错误。

curl_init() has been disabled for security reasons in /home/rasibaseport/public_html/config.php on line 38

1 个答案:

答案 0 :(得分:1)

您的代码对我有用。它的执行时间问题。添加以下代码以增加最长执行时间。

ini_set('max_execution_time', 300); //300 seconds = 5 minutes
ini_set('default_socket_timeout', 100); // 100 seconds = 1 Minutes 40 sec
//call the function file_get_html();

max_execution_time内部参数ini_set的解释

这将设置允许脚本在解析器终止之前运行的最长时间(以秒为单位)。这有助于防止编写不良的脚本占用服务器。 默认设置为30。有关详细信息,请阅读Runtime Configuration

要排除以下两个错误,请在第75行的fetch_http_file_contents($url)内使用file_get_contents($url)代替simple_html_dom.php

  

file_get_contents():无法打开流:无主机路由

     

file_get_contents():无法打开流:连接超时

function fetch_http_file_contents($url) {
  $hostname = parse_url($url, PHP_URL_HOST);
  if ($hostname == FALSE) {
    return FALSE;
  }

  $host_has_ipv6 = FALSE;
  $host_has_ipv4 = FALSE;
  $file_response = FALSE;

  $dns_records = dns_get_record($hostname, DNS_AAAA + DNS_A);

  foreach ($dns_records as $dns_record) {
    if (isset($dns_record['type'])) {
      switch ($dns_record['type']) {
        case 'AAAA':
          $host_has_ipv6 = TRUE;
          break;
        case 'A':
          $host_has_ipv4 = TRUE;
          break;
  } } }

  if ($host_has_ipv6 === TRUE) {
    $file_response = file_get_intbound_contents($url, '[0]:0');
  }
  if ($host_has_ipv4 === TRUE && $file_response == FALSE) {
    $file_response = file_get_intbound_contents($url, '0:0');
  }

  return $file_response;
}

function file_get_intbound_contents($url, $bindto_addr_family) {
  $stream_context = stream_context_create(
                      array(
                        'socket' => array(
                          'bindto' => $bindto_addr_family
                        ),
                        'http' => array(
                          'timeout'=>20,
                          'method'=>'GET'
                    ) ) );

  return file_get_contents($url, FALSE, $stream_context);
}

来源Making file_get_contents() more routing-robust and dual-stack

相关问题