Php 在CodeIgniter中使用文件获取内容
我对CodeIgniter非常陌生,我发现很难进入。我目前正在尝试在我的站点中使用以下API,以便我可以带回JSON文件并对其进行操作。就我所见,我应该让用户在视图中搜索口袋妖怪,将搜索传递回模型,并使用file_get_内容获取JSON文件,然后将其传递回视图进行操作 这是正确的吗?我该怎么做呢 到目前为止,我的CodeIgniter代码根本不起作用,但我可以在没有CodeIgniter的情况下这样做Php 在CodeIgniter中使用文件获取内容,php,ajax,json,codeigniter,Php,Ajax,Json,Codeigniter,我对CodeIgniter非常陌生,我发现很难进入。我目前正在尝试在我的站点中使用以下API,以便我可以带回JSON文件并对其进行操作。就我所见,我应该让用户在视图中搜索口袋妖怪,将搜索传递回模型,并使用file_get_内容获取JSON文件,然后将其传递回视图进行操作 这是正确的吗?我该怎么做呢 到目前为止,我的CodeIgniter代码根本不起作用,但我可以在没有CodeIgniter的情况下这样做 <!DOCTYPE HTML> <html> <head>
<!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>
及
所以我的问题是-我如何将其转换为与CodeIgniter一起工作?我也可以使用AJAX来避免页面刷新吗
谢谢。对于需要创建控制器方法的CI,此方法将您的代码设置为:
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));
}
然后,您需要创建一个view catch.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>
View 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>
View.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>
对于需要创建控制器方法的CI,此方法将代码放在以下位置:
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));
}
然后,您需要创建一个view catch.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>
View 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>
View.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的地方,我需要引用我的控制器和函数吗?不,它们是不同的,第二个是回应前面的评论。你不能像聊天那样简单地给出答案。编辑你的第一个答案并删除其中一个。是的!这里有推荐信。谢谢史密斯先生,我会安排的。哇,谢谢你!确认一下,你说yourhost/Pokemon/catchPokemon的地方,我需要引用我的控制器和函数吗?不,它们是不同的,第二个是回应前面的评论。你不能像聊天那样简单地给出答案。编辑你的第一个答案并删除其中一个。是的!这里有推荐信。谢谢史密斯先生,我会安排的。