【问题标题】:Having trouble with pulling an API key from riot从 riot 中提取 API 密钥时遇到问题
【发布时间】:2018-05-16 09:56:11
【问题描述】:

您好,我在 laravel 5.6 上拉动我的 Riot Api 时遇到问题。这是一个个人项目,我不认识其他了解 Laravel 的人。 我在 Riot 上重新注册以提取 API 密钥。

我收到此错误:

file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or 
service not known (View: 
/home/allyvoxc/ksgg.allyvox.co.za/resources/views/pages/LOL.blade.php)

突出显示的行是

$summoner = json_decode($result)->$summonerName;

这是我的代码,我在下面的代码中删除了我的 API 代码。

@extends('header')

@section('content')
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Summoner Spy</title>
</head>
<body>
<h1>Summoner Spy</h1>
<?php
$apiKey = 'This is my API code';
$summonerName = 'lolnexus';

// get the basic summoner info
$result = 
file_get_contents('https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/' 
. $summonerName . '?api_key=' . $apiKey);
$summoner = json_decode($result)->$summonerName;
// var_dump($summoner);
?>
<h3>
<image height="64" width="64" src="http://avatar.leagueoflegends.com/na/<? 
php print $summonerName; ?>.png" valign="middle"/>
<?php print $summonerName ?>
</h3>
<div>
Level: <?php print $summoner->summonerLevel; ?>
</div>

<?php
// get that summoner's wins and losses for each game type
$result = 
file_get_contents('https://na.api.pvp.net/api/lol/na/v1.3/stats/by- 
summoner/' . $summoner->id . '/summary?api_key=' . $apiKey);
$stats = json_decode($result);
// var_dump($stats);
foreach($stats->playerStatSummaries as $statSummary){
// $statSummary->losses: sometimes losses isn't set
$losses = property_exists($statSummary, 'losses')? $statSummary->losses : 
'(not available)';
print '<p><b>' . $statSummary->playerStatSummaryType . '</b>: ' .
    $statSummary->wins . ' wins, ' . $losses . ' losses</p>';
}
?>
</body>
</html>

感谢任何能够提供帮助的人

问候

【问题讨论】:

    标签: json api laravel-5 api-key


    【解决方案1】:

    我建议使用GuzzleHttp 包:http://docs.guzzlephp.org/en/stable/

    你的例子是:

    $client = new GuzzleHttp\Client();
    $res = $client->request('GET', 'https://na.api.pvp.net/api/lol/na/v1.3/stats/by-summoner/' . $summoner->id . '/summary', [
        'query' => [ 'api_key' => $apiKey ]
    ]);
    
    if($res->getStatusCode() == 200) {
        $json = json_decode($res->getBody());
        $summoner = $json->$summonerName
    }
    

    此外,我不确定这是否会成为您的最终代码。但是你设置代码的方式有点违背了像 Laravel 这样的 MVC 框架的目的。

    您应该在Controller 中收集所有数据,然后将这些数据传递给View

    我推荐这门课程从 Laravel 开始:https://laracasts.com/series/laravel-from-scratch-2017

    特别是针对您当前案例的第 5 集:https://laracasts.com/series/laravel-from-scratch-2017/episodes/5

    【讨论】:

    • 不,它不是最终代码,我将使用控制器,从一个基本方法开始,这样我就可以从那时开始工作并知道我可以在控制器中使用什么
    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 2019-09-06
    相关资源
    最近更新 更多