Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
用 PHP 构建 Pokémon API:初学者指南_创想鸟

用 PHP 构建 Pokémon API:初学者指南

用 php 构建 pokémon api:初学者指南

在本指南中,我们将逐步完成创建一个基本 php 项目的步骤,该项目将 pokémon api 与 flight 框架以及 zebra_curl 和 latte 等附加包结合使用。我们将探索设置项目、添加路线和渲染视图。

tl;dr:在 flight 中制作一个简单的基于 api 的项目并不难。查看本指南中使用的代码。

第 1 步:设置环境

首先,我们需要设置一个新的项目文件夹。打开终端,导航到所需位置,然后运行以下命令来创建新目录并输入它。

mkdir flight-pokeapicd flight-pokeapi

第 2 步:安装 composer

在深入研究代码之前,我们需要确保 composer 已安装。 composer 是 php 的依赖管理器,它将帮助我们包含必要的库。

如果您没有安装 composer,您可以使用以下命令安装它:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"php composer-setup.phpphp -r "unlink('composer-setup.php');"

现在 composer 已安装在名为 ./composer.phar 的当前文件中,让我们管理我们的依赖项。

立即学习“PHP免费学习笔记(深入)”;

第三步:安装依赖项

要管理所需的包,我们只需要使用composer添加它们即可。

./composer.phar require flightphp/core stefangabos/zebra_curl latte/latte

这将安装:

flight php:一个轻量级的 php 框架。zebra_curl:处理 http 请求的 curl 包装器。latte:用于渲染视图的模板引擎。

第四步:设置index.php

接下来,让我们为应用程序创建入口点:public/index.php。该文件将设置我们的应用程序、配置路由并处理视图的渲染。

创建public目录和index.php文件:

mkdir publictouch public/index.php

现在将以下内容添加到index.php:

settempdirectory(__dir__ . '/../temp');flight::map('render', function(string $template_path, array $data = []) use ($latte) {  $latte->render(__dir__ . '/../views/' . $template_path, $data);});// setup zebra_curl for handling http requests$curl = new zebra_curl();$curl->cache(__dir__ . '/../temp');flight::map('curl', function() use ($curl) {     return $curl; });// define a simple routeflight::route('/', function() {  echo 'hello world!';});flight::start();

在此文件中:

我们加载 composer 的自动加载器。设置 latte 以渲染视图。映射一个自定义渲染方法,该方法使用 latte 渲染 /views 文件夹中的模板。设置 zebra_curl 来处理 api 请求,并在我们想要调用它时将其映射到使用 flight::curl()。为主页 (/) 定义一个简单的路由,仅返回“hello world!”

如果您想测试此设置,您可以从公共目录启动 php 服务器:

php -s localhost:8000 -t public/

现在,在浏览器中访问 http://localhost:8000/,您应该会看到“hello world!”。酷吧?

第5步:添加路由

现在我们已经设置了基本路线,让我们添加一个使用 pokémon api 的更复杂的路线。更新 public/index.php 以包含以下代码:

flight::group('/pokemon', function(router $router) {    // route to list all pokémon types    $router->get('/', function() {        $types_response = json_decode(flight::curl()->scrap('https://pokeapi.co/api/v2/type/', true));        $results = [];        while ($types_response->next) {            $results = array_merge($results, $types_response->results);            $types_response = json_decode(flight::curl()->scrap($types_response->next, true));        }        $results = array_merge($results, $types_response->results);        flight::render('home.latte', [ 'types' => $results ]);    });});

我们创建了一个 /pokemon 路线组。路线组“包围”路线,并允许我们为组内的所有路线定义通用功能。/pokemon 路由通过使用 zebra_curl 从 pokémon api 获取所有可用的 pokémon 类型来列出它们。这还不能工作,因为我们需要添加 home.latte 视图来显示 pokémon 类型。

第 6 步:使用 latte 渲染视图

现在我们正在获取数据,让我们设置视图来显示它。创建views目录并添加latte模板文件以显示神奇宝贝类型。

mkdir viewstouch views/home.latte

将以下代码添加到views/home.latte:

welcome to the pokemon world!

types of pokemon

在此文件中:

我们循环遍历从路由传递过来的 $types 数组并显示每个 pokémon 类型的名称。

现在,访问 /pokemon 将显示所有神奇宝贝类型的列表!

第 7 步:分组并添加更多路线

让我们扩展 pokémon 路线,以获取特定类型和单个 pokémon 的更多详细信息。将以下路线添加到您的 /pokemon 组:

// route to fetch a specific pokémon type and list all associated pokémon$router->get('/type/@type', function(string $type) {    $curl = flight::curl();    $type_response = json_decode($curl->scrap('https://pokeapi.co/api/v2/type/' . $type, true));    $pokemon_urls = [];    foreach($type_response->pokemon as $pokemon_data) {        $pokemon_urls[] = $pokemon_data->pokemon->url;    }    $pokemon_data = [];    // the little & here is important to pass the variable by reference.    // in other words it allows us to modify the variable inside the closure.    $curl->get($pokemon_urls, function(stdclass $result) use (&$pokemon_data) {        $pokemon_data[] = json_decode($result->body);    });    flight::render('type.latte', [         'type' => $type_response->name,        'pokemons' => $pokemon_data    ]);});

在这条路线中,我们:

获取特定神奇宝贝类型的详细信息,包括所有相关的神奇宝贝。发送多个 api 请求以获取每个 pokémon 的详细信息。使用模板 (type.latte) 渲染数据。

接下来,创建 type.latte 视图:

touch views/type.latte

将以下内容添加到type.latte:

{$type|firstupper}

此模板显示与特定类型关联的每个神奇宝贝的名称。

第8步:有效果吗?

此时,您已经使用 flight php、用于 api 请求的 zebra_curl 和用于视图渲染的 latte 设置了基本的 pokémon api 使用者。您可以通过添加更多路线和完善模板来进一步扩展此项目。

要查看您的项目,请从公共目录启动 php 服务器:

php -s localhost:8000 -t public/

现在,在浏览器中访问 http://localhost:8000/pokemon,您应该会看到 pokémon 类型的列表。

故障排除

如果您需要帮助或遇到问题,您可以在 github 中查看完整代码,看看您可能在哪里犯了错误。

希望您喜欢这个小教程。如果您有任何疑问或需要帮助,请随时在下面的评论中提问。快乐编码!

以上就是用 PHP 构建 Pokémon API:初学者指南的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1246404.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
了解 PHP 中的静态成员
上一篇 2025年12月9日 18:33:49
PHP函数如何对参数进行验证和消毒?
下一篇 2025年12月9日 18:34:01

相关推荐

发表回复

登录后才能评论
关注微信