【发布时间】: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