2017-04-09 35 views
1

你好我主持我的网站到一个免费的托管网站(5gbfree.com),我在这里创建一个函数,它可以从网站http://ppa.com.ph??q=fcer_view那里检索比索美元的价格只需选择a.active元素并以明文形式获取该值。它昨天正在工作,但当我今天早上再次检查它时,它未能取回。因此,这将返回默认值是50简单的HTML DOM - 无法打开流:连接超时

error_log中:

[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 

我使用简单的HTML DOM。 这里有这个功能。

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; 
} 

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

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

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

编辑:我忘了提及用localhost(xampp)测试这个,期望的返回值是正确的,没有错误和警告。完美的工作。

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

[09-Apr-2017 20:18:53 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 46 

回答

1

您的代码适用于我。它的执行时间有问题。添加以下代码以增加最大执行时间。内部ini_set finction

参数max_execution_time

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

说明此设置以秒为脚本允许它被分析器终止之前运行的最大时间。这有助于防止编写糟糕的脚本捆绑服务器。 默认设置为30对于详细阅读Runtime Configuration

为了摆脱以下两个错误使用fetch_http_file_contents($url)代替file_get_contents($url)simple_html_dom.php上线75

的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

+0

太谢谢你了!你救了我的命。哈哈。你能解释一下,或者给我一个解释'ini_set('max_execution_time',秒)'的链接吗?只是有点好奇。无论如何,非常感谢你:) – legitghost

+1

@ghost我加入回答 –

+0

嗯,这很奇怪。我现在只是试了一下,而且没有再次工作。相同的错误日志error_log – legitghost