我的.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。
感谢任何帮助。谢谢!
答案 0 :(得分:1)
为什么要使用
header("Content-type: text/javascript");
如果要将它包含在index.php中,请在json_encoded.php中填写?!
答案 1 :(得分:1)
我的评论作为答案:
从json_encoded.php
中删除header("Content-type: text/javascript");