我想为我的应用程序创建一个 JSON 结构,所以我将我的代码上传到一个网络服务器并且一切正常。突然,它显示了这个警告:
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
我想问题出在服务器端,所以我在互联网上研究了这个问题,我发现也许 fopen 是不允许的,但它是,因为这段代码工作了好几次。据说问题与某些服务器端设置有关(我无法真正修改)。
这是代码:
$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));
?>