0

I want to make a JSON structure for my application, so I uploaded my code to a webserver and everything worked. 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. 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)); 
?>

4 Answers 4

2

I suggest you use curl to get contents through HTTP. Because if you choose curl, you can config all HTTP heads, time-out and so on. It's much more robust than 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;
    }
0

This means, the DNS server of your web server or the nameserver of this domain is not stable. Please check what is the DNS server your web server is using. Maybe change it to Google's DNS server: 8.8.8.8 or the DNS server of your ISP

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

0

Try using a website that is known to be up to test. Like google.

Or use Curl, if this dosnt work.

0

To use file_get_content to read a web address allow_url_fopen has to be set to 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_value allow_url_fopen On
1
  • If on an old hosting service, check safe_mode is not ON as well.
    – Cups
    Aug 17, 2013 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.