php - PHP代码被浏览器删除

标签 php json compiler-errors

我正在开发一个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/

相关文章:

php - OpenCart,显示商品总数

compiler-errors - Solidity - 标识符未找到或不唯一 onlyOwner()

php - 将 .JSON 导入 MySQL

php - 向自身提交页面最安全的方法是什么?

json - 实现自定义Newtonsoft JsonConverter(阅读器)

c - 在 C 程序中使用 JSON PUT

jquery - 在网络浏览器中使用 window.name 作为本地数据缓存

python-3.x - SConstruct-SyntaxError : Invalid Syntax

c++ - 对C++编译器错误感到困惑

php - 将 codesniffer 安装到 Aptana Studio 独立版本的正确方法是什么?