我正在开发一个PHP项目,该项目允许用户快速搜索《纽约时报》的文章-但是,由于某种原因,我的PHP无法进入显示的页面... chrome/firefox并未对其进行注释,代码不存在。我已经测试了helloworld.php,并仔细检查了我的xampp设置,一切都很好,所以我真的不知道该怎么做,而我的教授也很沮丧。
这是到目前为止的代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>NY Times Article Search</title>
<link rel=”stylesheet” href=”css/bootstrap.css” type=”text/css”/>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<script src="https://code.jquery.com/jquery.js"></script>
<script src=”js/bootstrap.min.js”></script>
<div class="container"> <h1><a href="index.php">NY Times Article Search</a></h1> </div>
<div class="navbar">
<div class="navbar-inner">
<div class="container">
<ul class="nav">
<li class="active"><a href="index.php">Home</a></li>
<li><a href="#">Recent Searches</a></li>
<li><a href="#">About</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<?php
require 'model/model.php';
$data = search_article('romney');
$json = json_decode($data);
var_dump($json);
print_r($json);
$url = $json->web_url;
$snippet = $json->snippet;
$headline = $json->headline;
echo $url;
?>
</div>
</body>
</html>
这是model.php
<?php
function search_article($keyword){
$base = "http://api.nytimes.com/svc/search/v2/articlesearch.json?";
$format = "json";
$api_key = "API_KEY";
$url = $base . "q=" . $keyword . "&api-key=" . $api_key;
$data = @file_get_contents($url);
return $data;
}
我试图使其保持准系统状态,所以我可以弄清楚这一点,但是没有运气...此外,如果代码很烂,对不起,我在网页设计方面也不擅长。
最佳答案
第一。从代码中删除@
,以查看错误。 file_get_contents($url);
也许您在$ keywords If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().
中有特殊字符
尝试$url = $base . "q=" . urlencode($keyword) . "&api-key=" . $api_key;
关于php - PHP代码被浏览器删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24017941/