file_get_contents偶尔会导致警告,但仍会提取数据

时间:2016-05-05 23:41:10

标签: php json ajax api

我的脚本大部分时间都在 ,但是在每8次尝试中我都会收到错误。我试着解释一下。这是我得到的错误(或类似的):

  

{" gameName":" F1 2011"," gameTrailer":" http://cdn.akamai.steamstatic.com/steam/应用/ 81163 / movie_max.webm吨= 1447354814""游戏ID":" 44360"" finalPrice":1499,"流派&# 34;:"赛车"}

     

{" gameName":" Starscape"" gameTrailer":" HTTP://cdn.akamai.steamstatic.com/steam/apps /900679/movie_max.webm?t=1447351523","gameId":"20700","finalPrice":999,"genres" ;:"动作"}

     

警告:file_get_contents(http://store.steampowered.com/api/appdetails?appids=400160):无法打开流:HTTP请求失败!在第19行的C:\ xampp \ htdocs \ GoStrap \ game.php

     

{" gameName":" DRAGON:关于龙的游戏"," gameTrailer":" http://cdn.akamai.steamstatic的.com /蒸汽/应用/ 2038811 / movie_max.webm吨= 1447373449""游戏ID":" 351150"" finalPrice":599,& #34;流派":"冒险"}

     

{" gameName":" Monster Mash"," gameTrailer":" http://cdn.akamai.steamstatic.com/steam/ ?应用/ 900919 / movie_max.webm吨= 1447352342""游戏ID":" 36210"" finalPrice":449,"流派&# 34;:"休闲"}

我正在制作一个从Steam商店获取随机Steam游戏信息的应用程序。这很简单。

  1. 该脚本从文本文件中获取(某种程度上)随机ID(确实有效)
  2. 将ID添加到API的URL末尾,并使用file_get_contents来获取文件。然后它解码json。 (可能是某种程度上的问题)
  3. 搜索我的指定数据。最终价格&电影webm并不总是存在,因此if(!isset())
  4. 决定最终价格并在index.php上发回ajax
  5. 上面的错误代码表明我在4种情况下得到了我需要的数据,并且错误一次。我只想收到一个json字符串并返回它,并且只设置了$game['gameTrailer']$game['final_price']

    这是php(它不是很好,很善良):

    <?php
    //Run the script on ajax call
    if(isset($_POST)) {
        fetchGame();
    }
    
    function fetchGame() {
    
        $gameFound = false;
    
        while(!$gameFound) {
            ////////// ID-picker //////////
            $f_contents = file("steam.txt");
            $url = $f_contents[mt_rand(0, count($f_contents) - 1)];             
            $answer = explode('/',$url);
            $gameID = $answer[4];
            $trimmed = trim($gameID);
    
            ////////// Fetch game //////////
            $json = file_get_contents('http://store.steampowered.com/api/appdetails?appids='.$trimmed);
            $game_json = json_decode($json, true);
    
            if(!isset($game_json[$trimmed]['data']['movies'][0]['webm']['max']) || !isset($game_json[$trimmed]['data']['price_overview']['final'])) {
                continue;
            }
    
            $gameFound = true;
    
            ////////// Store variables //////////
            $game['gameName'] = $game_json[$trimmed]['data']['name'];
            $game['gameTrailer'] = $game_json[$trimmed]['data']['movies'][0]['webm']['max'];
            $game['gameId'] = $trimmed;
            $game['free'] = $game_json[$trimmed]['data']['is_free'];
            $game['price'] = $game_json[$trimmed]['data']['price_overview']['final'];
            $game['genres'] = $game_json[$trimmed]['data']['genres'][0]['description'];
    
            if ($game['free'] == TRUE) {
                $game['final_price'] = "Free";
            } elseif($game['free'] == FALSE || $game['final_price'] != NULL) {
                $game['final_price'] = $game['price'];
            } else {
                $game['final_price'] = "-";
            }
        }
    
        ////////// Return to AJAX (index.php) //////////
        echo 
            json_encode(array(
                'gameName' => $game['gameName'],
                'gameTrailer' => $game['gameTrailer'],
                'gameId' => $game['gameId'],
                'finalPrice' => $game['final_price'],
                'genres' => $game['genres'],
            ))
        ;
    }
    
    ?>
    

    任何帮助将不胜感激。比如,为什么会发生这种情况有明显的原因吗?有没有明显更好的方法?当它似乎已经获取了我需要的数据时,为什么它至少重复迭代4次?对不起,如果这篇文章很长,只是试着用缺少的php / json词汇来详细说明。

    亲切的问候,约翰

    修改

    有时它不会返回错误,只返回多个对象:

      

    {&#34; gameName&#34;:&#34; Prime World:Defender&#34;,&#34; gameTrailer&#34;:&#34; http://cdn.akamai.steamstatic.com/蒸汽/应用/ 2028642 / movie_max.webm吨= 1447357836&#34;&#34;游戏ID&#34;:&#34; 235360&#34;&#34; finalPrice&#34;:899,&#34;流派&#34;:&#34;休闲&#34;}

         

    {&#34; gameName&#34;:&#34; Grand Ages:Rome&#34;,&#34; gameTrailer&#34;:&#34; http://cdn.akamai.steamstatic.com/蒸汽/应用/ 5190 / movie_max.webm吨= 1447351683&#34;&#34;游戏ID&#34;:&#34; 23450&#34;&#34; finalPrice&#34;:999,&#34;流派&#34;:&#34;模拟&#34;}

0 个答案:

没有答案
相关问题