file_get_contents如何不显示是否需要数组

时间:2016-04-01 17:41:57

标签: php

我试图让我的代码在发送到的url没有所需的变量时工作。

这是错误:

  

E_WARNING:type 2 - file_get_contents(链接):无法打开流:HTTP请求失败!未找到HTTP / 1.1 404 - 第23行

我的代码转到此用户页面,一切正常:

https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/24267598/ranked?season=SEASON2016&api_key=e9044828-20e3-46cc-9eb5-545949299803

但是当它转到此用户页面时会出错:

https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/77828400/ranked?season=SEASON2016&api_key=e9044828-20e3-46cc-9eb5-545949299803

如果在网址中没有任何内容,因为它没有显示任何内容以及何时显示它,我会做什么。但出于某种原因,我无法让它与两者合作。

这是我的代码:

<?php
$apiKey = 'APIKEY';
$summonerName = 'raget deathdex';
$new = rawurlencode($summonerName);

$news = str_replace(' ', '', $summonerName);
$str = strtolower($news);

$result = file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/' . $new . '?api_key=' . $apiKey);
$summoner = json_decode($result)->$str;
$id = $summoner->id;
?>  

<?php   
$claw = file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/' . $id . '/ranked?season=SEASON2016&api_key=' . $apiKey);
$gaza = json_decode($claw);
?>

<?php 
$entriesz = $gaza->champions;
usort($entriesz, function($ac,$bc){
    return $bc->stats->totalSessionsPlayed-$ac->stats->totalSessionsPlayed;
});

foreach($entriesz as $statSummaryz) if ($tmp++ < 11){

    $getLeagueNamelistside = $statSummaryz->id;
    $getsessionsplayedNamelistside = $statSummaryz->stats->totalSessionsPlayed;
    $getMiniomskillsNamelistside = $statSummaryz->stats->totalMinionKills;
    $getkillsNamelistside = $statSummaryz->stats->totalChampionKills;
    $getassistssNamelistside = $statSummaryz->stats->totalAssists;
    $getdeathsNamelistside = $statSummaryz->stats->totalDeathsPerSession;
    $getlosseslistside = $statSummaryz->stats->totalSessionsLost;
    $getwinslistside = $statSummaryz->stats->totalSessionsWon;

    $Percentkillrateside = $getkillsNamelistside / $getsessionsplayedNamelistside;
    $Percentassistrateside = $getassistssNamelistside / $getsessionsplayedNamelistside;
    $Percentdeathrateside = $getdeathsNamelistside / $getsessionsplayedNamelistside;
    $KDAside = ($getkillsNamelistside + $getassistssNamelistside) / $getdeathsNamelistside;
    $KDAMinniomsSide = $getMiniomskillsNamelistside / $getsessionsplayedNamelistside;
    $PercentWinRateSide = 100 / ($getlosseslistside + $getwinslistside) * $getwinslistside;

    if ($getLeagueNamelistside >=1){

        $resultz = file_get_contents('https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion/'.$getLeagueNamelistside.'?api_key=' . $apiKey);
        $summonerz = json_decode($resultz, true);
        $getLeagueNamelistsidez = $summonerz['name'];
        $getLeagueKeyNamelistsidez = $summonerz['key'];
        echo '<p><img src="http://lolchecker.esy.es/LOLGGWP/img/champion/' .$getLeagueKeyNamelistsidez. '.png"></p>'.$getLeagueNamelistsidez. '<p> Kills '.number_format((float)$Percentkillrateside, 1, '.', '').'</p><p> Deaths '.number_format((float)$Percentdeathrateside, 1, '.', '').'</p><p> Assists '.number_format((float)$Percentassistrateside, 1, '.', '').'</p><p> KDA '.number_format((float)$KDAside, 2, '.', '').':1 KDA</p><p> CS '.number_format((float)$KDAMinniomsSide, 1, '.', '').' CS</p><p> Games Played '.$getsessionsplayedNamelistside.'</p><p> Win Rate '.number_format((float)$PercentWinRateSide, 0, '.', '').'%</p>';

    }
    elseif($getLeagueNamelistside =0){
        return DO_NOTHING;

    }

}
?>

3 个答案:

答案 0 :(得分:0)

这是你想要做的吗?

PHP将字符串视为字节数组。如果字符串长度大于0,而不是检查字符串是否为空,我想检查是否设置了第一个字节。有些人认为它不那么人性化,但仅仅是某些人

使用isset($mystring[0])查看字符串的长度是否至少为一个字符。

file_get_contents之前的@符号用于错误抑制。如果无法找到文件,@会阻止错误显示在浏览器中。

<?php
$apiKey = 'e9044828-20e3-46cc-9eb5-545949299803';
$summonerName = 'raget deathdex';
$new = rawurlencode($summonerName);

$news = str_replace(' ', '', $summonerName);
$str = strtolower($news);


$result = @file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/' . $new . '?api_key=' . $apiKey);
if(!isset($result[0])) {
    die(); // die('Nothing found in \'result\' file.');
}

$summoner = json_decode($result)->$str;
if(!$summoner) {
    die(); // die('Nothing found in \'result\' content.');
}

$id = $summoner->id;

?>  



<?php   
$claw = @file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/' . $id . '/ranked?season=SEASON2016&api_key=' . $apiKey);
if(!isset($claw[0])) {
    die(); // die('Nothing found in \'claw\' file.');
}

$gaza = json_decode($claw);
if(!$gaza) {
    die(); // die('Nothing found in \'claw\' content.');
}
?>


<?php 
$entriesz = $gaza->champions;
usort($entriesz, function($ac,$bc){
return $bc->stats->totalSessionsPlayed-$ac->stats->totalSessionsPlayed;
});

foreach($entriesz as $statSummaryz) if ($tmp++ < 11){

$getLeagueNamelistside = $statSummaryz->id;
$getsessionsplayedNamelistside = $statSummaryz->stats->totalSessionsPlayed;
$getMiniomskillsNamelistside = $statSummaryz->stats->totalMinionKills;
$getkillsNamelistside = $statSummaryz->stats->totalChampionKills;
$getassistssNamelistside = $statSummaryz->stats->totalAssists;
$getdeathsNamelistside = $statSummaryz->stats->totalDeathsPerSession;
$getlosseslistside = $statSummaryz->stats->totalSessionsLost;
$getwinslistside = $statSummaryz->stats->totalSessionsWon;



$Percentkillrateside = $getkillsNamelistside / $getsessionsplayedNamelistside;
$Percentassistrateside = $getassistssNamelistside / $getsessionsplayedNamelistside;
$Percentdeathrateside = $getdeathsNamelistside / $getsessionsplayedNamelistside;
$KDAside = ($getkillsNamelistside + $getassistssNamelistside) / $getdeathsNamelistside;
$KDAMinniomsSide = $getMiniomskillsNamelistside / $getsessionsplayedNamelistside;
$PercentWinRateSide = 100 / ($getlosseslistside + $getwinslistside) * $getwinslistside;


if ($getLeagueNamelistside >=1){

    $resultz = @file_get_contents('https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion/'.$getLeagueNamelistside.'?api_key=' . $apiKey);
    if(!isset($resultz[0])) {
        die(); // die('Nothing found in league file:' . $getLeagueNamelistside);
    }

$summonerz = json_decode($resultz, true);
if(!summonerz) {
    die(); // die('Nothing found in league content:' . $getLeagueNamelistside);
}

$getLeagueNamelistsidez = $summonerz['name'];
$getLeagueKeyNamelistsidez = $summonerz['key'];
    echo '<p><img src="http://lolchecker.esy.es/LOLGGWP/img/champion/' .$getLeagueKeyNamelistsidez. '.png"></p>'.$getLeagueNamelistsidez. '<p> Kills '.number_format((float)$Percentkillrateside, 1, '.', '').'</p><p> Deaths '.number_format((float)$Percentdeathrateside, 1, '.', '').'</p><p> Assists '.number_format((float)$Percentassistrateside, 1, '.', '').'</p><p> KDA '.number_format((float)$KDAside, 2, '.', '').':1 KDA</p><p> CS '.number_format((float)$KDAMinniomsSide, 1, '.', '').' CS</p><p> Games Played '.$getsessionsplayedNamelistside.'</p><p> Win Rate '.number_format((float)$PercentWinRateSide, 0, '.', '').'%</p>';



}
 elseif($getLeagueNamelistside == 0){
    die();
   }
}
?>
<?php
$apiKey = 'e9044828-20e3-46cc-9eb5-545949299803';
$summonerName = 'raget deathdex';

// =====

$results = @file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/' . rawurlencode($summonerName) . '?api_key=' . $apiKey);
if(!isset($results[0]))
    die();

$summoner = json_decode($results)->strtolower(str_replace(' ', '', $summonerName));
if(!$summoner)
    die();

$results = @file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/' . $summoner->id . '/ranked?season=SEASON2016&api_key=' . $apiKey);
if(!isset($results[0]))
    die();

$gaza = json_decode($results);
if(!$gaza)
    die();

// =====

$entriesz = $gaza->champions;
usort($entriesz, function($ac, $bc){
    return $bc->stats->totalSessionsPlayed-$ac->stats->totalSessionsPlayed;
});

// =====

foreach($entriesz as $statSummaryz) {
    if($tmp++ < 11) {
        $getLeagueNamelistside = $statSummaryz->id;
        $getsessionsplayedNamelistside = $statSummaryz->stats->totalSessionsPlayed;
        $getMiniomskillsNamelistside = $statSummaryz->stats->totalMinionKills;
        $getkillsNamelistside = $statSummaryz->stats->totalChampionKills;
        $getassistssNamelistside = $statSummaryz->stats->totalAssists;
        $getdeathsNamelistside = $statSummaryz->stats->totalDeathsPerSession;
        $getlosseslistside = $statSummaryz->stats->totalSessionsLost;
        $getwinslistside = $statSummaryz->stats->totalSessionsWon;

        $Percentkillrateside = $getkillsNamelistside / $getsessionsplayedNamelistside;
        $Percentassistrateside = $getassistssNamelistside / $getsessionsplayedNamelistside;
        $Percentdeathrateside = $getdeathsNamelistside / $getsessionsplayedNamelistside;
        $KDAside = ($getkillsNamelistside + $getassistssNamelistside) / $getdeathsNamelistside;
        $KDAMinniomsSide = $getMiniomskillsNamelistside / $getsessionsplayedNamelistside;
        $PercentWinRateSide = 100 / ($getlosseslistside + $getwinslistside) * $getwinslistside;

        if($getLeagueNamelistside >= 1) {
            $results = @file_get_contents('https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion/' . $getLeagueNamelistside . '?api_key=' . $apiKey);
            if(!isset($results[0]))
                die();
        }

        $summonerz = json_decode($results, true);
        if(!$summonerz)
            die();

        $getLeagueNamelistsidez = $summonerz['name'];
        $getLeagueKeyNamelistsidez = $summonerz['key'];
        echo 
            '<p><img src="http://lolchecker.esy.es/LOLGGWP/img/champion/', $getLeagueKeyNamelistsidez, '.png"></p>',
            $getLeagueNamelistsidez,
            '<p> Kills ', number_format((float)$Percentkillrateside, 1, '.', ''),
            '</p><p> Deaths ', number_format((float)$Percentdeathrateside, 1, '.', ''),
            '</p><p> Assists ', number_format((float)$Percentassistrateside, 1, '.', ''),
            '</p><p> KDA ', number_format((float)$KDAside, 2, '.', ''),
            ':1 KDA</p><p> CS ', number_format((float)$KDAMinniomsSide, 1, '.', ''),
            ' CS</p><p> Games Played ', $getsessionsplayedNamelistside,
            '</p><p> Win Rate ', number_format((float)$PercentWinRateSide, 0, '.', ''),
            '%</p>', "\n";
    }
    elseif($getLeagueNamelistside == 0)
        die();
}

答案 1 :(得分:0)

您应该能够检查文件内容,以确定它具有所需的数据。

<?php   
$claw = file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/' . $id . '/ranked?season=SEASON2016&api_key=' . $apiKey);
if(false === empty($claw)) {
    $gaza = json_decode($claw);
    if(true === empty($gaza->champion)) {
        return 'Invalid data received';
    }
} else {
    return 'No data found';
}
?>

您的比较条件似乎也存在语法错误,该错误使用赋值运算符,该运算符将始终求值为true。但不确定是否使用了您的代码部分。

elseif($getLeagueNamelistside =0){

应该只是

else {

将作为if ($getLeagueNamelistside <= 0) return DO_NOTHING;

<强>更新

要防止显示文件错误,并显示其他错误,您可以禁用错误消息的显示。无论如何,生产代码应该是这种情况。

ini_set('display_errors', 'off');

或过滤仅显示某些错误消息。

ini_set('error_reporting', E_ERROR | E_PARSE);

您还可以通过预先@

来绕过特定命令的错误报告
@file_get_contents('https://example.com/index.php');

最后但并非最不重要的,我的首选方法是使用输出缓冲来防止不需要的内容输出到客户端。

ob_start();
echo file_get_contents('https://example.com/index.php');
$content = ob_get_clean();
echo false === strpos($content, '404') ? $content : 'OOps';

答案 2 :(得分:0)

试试这个;)

$news = str_replace(' ', '', $summonerName);
$str  = strtolower($news);

$result   = file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/' . $new . '?api_key=' . $apiKey);
$summoner = json_decode($result)->$str;
$id       = $summoner->id;

$claw = file_get_contents('https://euw.api.pvp.net/api/lol/euw/v1.3/stats/by-summoner/' . $id . '/ranked?season=SEASON2016&api_key=' . $apiKey);
$gaza = json_decode($claw);
if (isset($gaza->champions)) {
  $entriesz = $gaza->champions;
  usort($entriesz, function($ac, $bc) {
    return $bc->stats->totalSessionsPlayed - $ac->stats->totalSessionsPlayed;
  });

  foreach ($entriesz as $statSummaryz) {
    if ($tmp++ < 11) {
      $getLeagueNamelistside         = $statSummaryz->id;
      $getsessionsplayedNamelistside = $statSummaryz->stats->totalSessionsPlayed;
      $getMiniomskillsNamelistside   = $statSummaryz->stats->totalMinionKills;
      $getkillsNamelistside          = $statSummaryz->stats->totalChampionKills;
      $getassistssNamelistside       = $statSummaryz->stats->totalAssists;
      $getdeathsNamelistside         = $statSummaryz->stats->totalDeathsPerSession;
      $getlosseslistside             = $statSummaryz->stats->totalSessionsLost;
      $getwinslistside               = $statSummaryz->stats->totalSessionsWon;

      $Percentkillrateside   = $getkillsNamelistside / $getsessionsplayedNamelistside;
      $Percentassistrateside = $getassistssNamelistside / $getsessionsplayedNamelistside;
      $Percentdeathrateside  = $getdeathsNamelistside / $getsessionsplayedNamelistside;
      $KDAside               = ($getkillsNamelistside + $getassistssNamelistside) / $getdeathsNamelistside;
      $KDAMinniomsSide       = $getMiniomskillsNamelistside / $getsessionsplayedNamelistside;
      $PercentWinRateSide    = 100 / ($getlosseslistside + $getwinslistside) * $getwinslistside;

      if ($getLeagueNamelistside >= 1) {
        $resultz                   = file_get_contents('https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion/' . $getLeagueNamelistside . '?api_key=' . $apiKey);
        $summonerz                 = json_decode($resultz, true);
        $getLeagueNamelistsidez    = $summonerz['name'];
        $getLeagueKeyNamelistsidez = $summonerz['key'];
        echo '<p><img src="http://lolchecker.esy.es/LOLGGWP/img/champion/' . $getLeagueKeyNamelistsidez . '.png"></p>' . $getLeagueNamelistsidez . '<p> Kills ' . number_format((float) $Percentkillrateside, 1, '.', '') . '</p><p> Deaths ' . number_format((float) $Percentdeathrateside, 1, '.', '') . '</p><p> Assists ' . number_format((float) $Percentassistrateside, 1, '.', '') . '</p><p> KDA ' . number_format((float) $KDAside, 2, '.', '') . ':1 KDA</p><p> CS ' . number_format((float) $KDAMinniomsSide, 1, '.', '') . ' CS</p><p> Games Played ' . $getsessionsplayedNamelistside . '</p><p> Win Rate ' . number_format((float) $PercentWinRateSide, 0, '.', '') . '%</p>';
      } elseif ($getLeagueNamelistside == 0) {
        return DO_NOTHING;
      }
    }
  }
}