我的 .php 文件中的代码在浏览器中显示。我正在使用 XAMPP 服务器运行 apache 2.4 并添加<?php phpinfo(); ?>
作品,所以我猜测 php 5.4 版也可以正常工作。我的 index.php 文件如下所示:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Something</title>
</head>
<body>
<?php
include 'json_encoded.php';
?>
<script type='text/javascript'>
/* @var $image_urls type */
var jsarray = ["<?php echo join("\", \"", $image_urls); ?>"];
//document.write(getlength(jsarray));
</script>
</body>
</html>
我的 json_encoded.php 文件如下所示:
<?php
header("Content-type: text/javascript");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$url = 'some url';
$var = fread_url($url);
preg_match_all ("@((http://web)([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)@",
$var, $matches);
$matches = $matches[1];
$image_urls = array();
foreach($matches as $var)
{
$var1 = str_replace("/med/", "/lg/", $var);
$image_urls[] = $var1;
}
// The fread_url function allows you to get a complete
// page. If CURL is not installed replace the contents with
// a fopen / fget loop
function fread_url($url,$ref="")
{
if(function_exists("curl_init")){
$ch = curl_init();
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; ".
"Windows NT 5.0)";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt( $ch, CURLOPT_HTTPGET, 1 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION , 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_REFERER, $ref );
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$html = curl_exec($ch);
curl_close($ch);
}
else{
$hfile = fopen($url,"r");
if($hfile){
while(!feof($hfile)){
$html.=fgets($hfile,1024);
}
}
}
return $html;
}
?>
和我在运行 localhost:... 时的输出看起来像这样
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Today's Front Pages</title>
</head>
<body>
<script type='text/javascript'>
/* @var $image_urls type */
var jsarray = [...contents of array];
//document.write(getlength(jsarray));
</script>
</body>
</html>
有谁知道我做错了什么?我看到了几篇关于该主题的帖子,但他们都表示没有正确安装 php,但我的情况并非如此。如果它很重要,我将使用 NetBeans 7.2 作为我的 IDE。
任何帮助表示赞赏。谢谢!