在CodeIgniter中使用file_get_contents

时间:2014-03-19 16:23:34

标签: php ajax json codeigniter

我对CodeIgniter很新,我发现很难进入。我目前正在尝试在我的网站中使用以下API(http://pokeapi.co),以便我可以恢复JSON文件并对其进行操作。据我所知,我应该让用户在视图中搜索一个Pokemon,将搜索传递回模型并使用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来避免页面刷新吗?

谢谢。

1 个答案:

答案 0 :(得分:4)

对于CI,您需要创建一个Controller方法,这个方法,您将代码设置为:

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>
相关问题