连接丢失时显示错误消息

时间:2018-03-01 09:45:19

标签: php

我有表格,我用它来回复某些货币...

基本上,我正在使用file_get_contents()函数,如果我失去连接,我会收到此错误:

  

php_network_getaddresses:getaddrinfo failed:没有这样的主机。   在

是否有可能回显一些自定义错误?当用户在尝试提交表单时失去连接时..

我的PHP代码:

<?php error_reporting (E_ALL ^ E_NOTICE); ?>

<?php
$params = $_REQUEST;
//print_R($params);die('fff');
if(isset($params) && isset($params['amount']) && isset($params['from']) && isset($params['from'])) {
    currencyConverter($params['from'], $params['to'], $params['amount']);
}

function currencyConverter($from_currency,$to_currency,$amount) {
    $amount = urlencode($amount);
    $from_currency = urlencode($from_currency);
    $to_currency = urlencode($to_currency);
    $get = file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from_currency&to=$to_currency");
    $get = explode("<span class=bld>",$get);
    $get = explode("</span>",$get[1]);
    $converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]);

    echo $converted_currency;

    if($from_currency == $to_currency){


      echo "try again";
    }


}

1 个答案:

答案 0 :(得分:1)

你应该抓住file_get_contents函数的错误。失败时返回false。 这是它的文档。

http://php.net/manual/en/function.file-get-contents.php

$get = @file_get_contents("https://finance.google.com/finance/converter?a=$amount&from=$from_currency&to=$to_currency");
if(!$get){
   echo "My custom error";
}