【问题标题】:Using file_get_contents in CodeIgniter在 CodeIgniter 中使用 file_get_contents
【发布时间】:2014-04-26 01:35:48
【问题描述】:

我是 CodeIgniter 的新手,我发现很难进入。我目前正在尝试在我的网站内使用以下 API (http://pokeapi.co),以便我可以带回 JSON 文件并对其进行操作。据我所知,我应该让用户在视图中搜索口袋妖怪,将搜索传递回模型并使用 file_get_contents 获取 JSON 文件,然后将其传递回视图进行操作。

这是正确的吗?我该怎么做呢?

到目前为止,我的 CodeIgniter 代码根本不起作用,但我可以在没有 CodeIgniter 的情况下做到这一点。

<!DOCTYPE HTML>
<html>
<head>
    <title>The Pokedex!</title>
</head>
<body>  
    <form name="search" action="api_test.php" method="POST">
        <label>Please search for a Pokemon</label>
        <input type="search" name="pokemon" required>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

和;

<?php

$pokemon = strtolower($_POST['pokemon']);
$siteaddressAPI = "http://pokeapi.co/api/v1/pokemon/" . $pokemon . "/";
$data = file_get_contents($siteaddressAPI);

?>

<!DOCTYPE HTML>
<html>
<body>
    <script>
        var obj = <?php echo $data; ?>;     

        document.write("<p>Name: " + obj.name + "</p>");
        document.write("<p>Pokedex Number: " + obj.national_id + "</p>");               
        document.write("<p>Height: " + obj.height + "</p>");
        document.write("<p>Weight: " + obj.weight + "</p>");            
    </script>
</body>

所以我的问题确实是 - 我如何将其转换为与 CodeIgniter 一起使用?我也可以使用 AJAX 来避免页面刷新吗?

谢谢。

【问题讨论】:

    标签: php ajax json codeigniter


    【解决方案1】:

    对于CI你需要创建一个Controller Method,这个方法,你把你的代码写成:

    public function catchPokemon($pokemon)
    {
        $pokemon = strtolower($pokemon);
        $siteaddressAPI = "http://pokeapi.co/api/v1/pokemon/" . $pokemon . "/";
        $data = file_get_contents($siteaddressAPI);
    
        $this->load->view('catches', json_decode($data));
    }
    

    然后您需要创建一个视图 (catches.php) 来分析数据,例如:

    <!DOCTYPE HTML>
    <html>
    <body>
        <p>Name: <strong><?php echo $name; ?></strong></p>
        <p>Pokedex Number: <strong><?php echo $national_id; ?></strong></p>
        <p>Height: <strong><?php echo $height; ?></strong></p>
        <p>Weight: <strong><?php echo $weight; ?></strong></p>
    </body>
    

    现在,您可以使用 jQuery 通过 ajax 请求调用控制器方法。

    即:

    控制器

    class Pokemon extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
        }
    
        public function index()
        {
            $this->load->view('index');
        }
    
        /**
         * Calls with http://yourhost/Pokemon/catchPokemon/PokemonName
         */
        public function catchPokemon($pokemon)
        {
            $pokemon = strtolower($pokemon);
            $siteaddressAPI = "http://pokeapi.co/api/v1/pokemon/" . $pokemon . "/";
            $data = file_get_contents($siteaddressAPI);
    
            $this->load->view('catches', json_decode($data));
        }
    }
    

    查看 index.php

    <!DOCTYPE HTML>
    <html>
    <head>
        <title>The Pokedex!</title>
        ......Scripts and styles references......
        <script>
            $(function(){
                var finish = function(data){
                    $('#poke').html(data);
                };
    
                $('#search').submit(function(){
                    $.get("http://yourhost/Pokemon/catchPokemon/" +
                            $('#pokemon').val(), finish);
    
                    return false;
                });
            });
        </script>
    </head>
    <body>  
        <form id="search">
            <label>Please search for a Pokémon</label>
            <input type="search" name="pokemon" required>
            <input type="submit" value="Submit">
        </form>
        <div id="poke"></div>
    </body>
    </html>
    

    查看catches.php

    <!DOCTYPE HTML>
    <html>
    <body>
        <p>Name: <strong><?php echo $name; ?></strong></p>
        <p>Pokedex Number: <strong><?php echo $national_id; ?></strong></p>
        <p>Height: <strong><?php echo $height; ?></strong></p>
        <p>Weight: <strong><?php echo $weight; ?></strong></p>
    </body>
    

    【讨论】:

    • 哇,谢谢!只是为了确认一下,在你说“yourhost/Pokemon/catchPokemon”的地方,我需要引用我的 Contoller 并在那里工作吗?
    • 不,它们是不同的,第二个是回应之前的评论。
    • 你不能像聊天那样只放答案。编辑您的第一个答案并删除其中一个。
    • 是的!参考那里。
    • 谢谢史密斯先生,我会安排的。
    猜你喜欢
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2014-01-07
    相关资源
    最近更新 更多