简体   繁体   English

在PHP中突然发出file_get_contents警告

[英]file_get_contents warning suddenly in PHP

I want to make a JSON structure for my application, so I uploaded my code to a webserver and everything worked. 我想为我的应用程序创建一个JSON结构,所以我将我的代码上传到网络服务器,一切正常。 Suddenly, it showed this warning: 突然,它显示了这个警告:

Warning: file_get_contents(): Couldn't resolve host name in /home/u815921584/public_html/slideshow.php on line 15

Warning: file_get_contents(http://www.webpage.com): failed to open stream: operation failed in /home/u815921584/public_html/slideshow.php on line 15

I suppose the problem is on server side, so I researched this problem on the internet, and I found that maybe fopen is not allowed, but IT IS, because this code worked several times. 我想问题出在服务器端,所以我在互联网上研究了这个问题,我发现可能不允许使用fopen,但事实是,因为这段代码工作了好几次。 Supposedly the problem has to do something with some server side settings (which I can't really modify). 据说问题必须与某些服务器端设置有关(我无法真正修改)。

And this is the code: 这是代码:

$fileInString = file_get_contents("http://www.webpage.com");


$divStart = "<div id=\"featured\">";
$divEnd = "</div>";
$imgStart = "<img src='";
$imgEnd = "'";

$currentPoint = 0;
$tomb = array();

/* Parsing */
// Parsing image  
$currentPoint = strpos($fileInString, $divStart, $currentPoint);
$currentPoint += (strlen($divStart)) ;
$endPoint =  strpos($fileInString, $divEnd, $currentPoint);
$img_text = substr($fileInString, $currentPoint, $endPoint - $currentPoint);
$currentPoint = $endPoint + strlen($divEnd);

    // Subparsing image
$innerPoint = strpos($img_text, $imgStart, 0);
  $innerPoint += strlen($imgStart);
  $innerEndPoint = strpos($img_text, $imgEnd, $innerPoint);
  $img_1 = "http://www.webpage.com".substr($img_text, $innerPoint, $innerEndPoint - $innerPoint);
  $innerEndPoint += strlen($imgEnd);

$innerPoint = strpos($img_text, $imgStart, $innerEndPoint);
  $innerPoint += strlen($imgStart);
  $innerEndPoint = strpos($img_text, $imgEnd, $innerPoint);
  $img_2 = "http://www.webpage.com".substr($img_text, $innerPoint, $innerEndPoint - $innerPoint);
  $innerEndPoint += strlen($imgEnd);

 $innerPoint = strpos($img_text, $imgStart, $innerEndPoint);
  $innerPoint += strlen($imgStart);
  $innerEndPoint = strpos($img_text, $imgEnd, $innerPoint);
  $img_3 = "http://www.webpage.com".substr($img_text, $innerPoint, $innerEndPoint - $innerPoint);
  $innerEndPoint += strlen($imgEnd);

    $tomb[0] = array("img1" => $img_1, "img2" => $img_2, "img3" => $img_3);



echo str_replace('\/','/',json_encode($tomb)); 
?>

I suggest you use curl to get contents through HTTP. 我建议你使用curl通过HTTP获取内容。 Because if you choose curl , you can config all HTTP heads, time-out and so on. 因为如果选择curl ,则可以配置所有HTTP头,超时等。 It's much more robust than file_get_contents 它比file_get_contents更强大

  function get($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url); //set url
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // get content as string
        $data = curl_exec($ch); //execute 
        curl_close($ch); // close hander
        return $data;
    }

This means, the DNS server of your web server or the nameserver of this domain is not stable. 这意味着,您的Web服务器的DNS服务器或此域的名称服务器不稳定。 Please check what is the DNS server your web server is using. 请检查您的Web服务器正在使用的DNS服务器。 Maybe change it to Google's DNS server: 8.8.8.8 or the DNS server of your ISP 也许将其更改为Google的DNS服务器:8.8.8.8或您的ISP的DNS服务器

If name server is not stable, you need to contact your name server provider or change it to Amazon Route 53 server and try again 如果名称服务器不稳定,您需要联系您的名称服务器提供商或将其更改为Amazon Route 53服务器,然后重试

Try using a website that is known to be up to test. 尝试使用已知要测试的网站。 Like google. 像谷歌一样。

Or use Curl, if this dosnt work. 或者使用Curl,如果这个dosnt工作。

To use file_get_content to read a web address allow_url_fopen has to be set to on. 要使用file_get_content读取Web地址,必须将allow_url_fopen设置为on。 Have you checked that? 你检查过了吗?

If you do not have access to the php.ini on a hosted service you can turn it on from the .htaccess file using 如果您无法访问托管服务上的php.ini,则可以使用.htaccess文件将其打开

php_value allow_url_fopen On

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM