【问题标题】:Looking for something in json file then trying something else if it doesnt work在 json 文件中寻找一些东西,如果它不起作用,则尝试其他东西
【发布时间】:2015-08-01 02:30:54
【问题描述】:

我正在从一个站点获取 json 文件,我希望它找到哪个获取该文件并使用该文件。我也在使用 $_GET 方法。有两种方法可以获取相同的文件,但它们都需要一个 id 或来自 steam 的自定义 url。

我的网址示例:www.mysite.com?id=123123123

2种获取json文件的方式:

我是如何解码的:

$id = $_GET['id'];
$url = "http://steamcommunity.com/id/".$id."/inventory/json/730/2";
$content = file_get_contents($url);
$playerinfo = json_decode($content, true);
$InventoryStatus = $playerinfo['success'];

【问题讨论】:

  • 有什么问题?代码不工作?
  • @borracciaBlu 我找不到它,所以它会尝试其中一个 url,看看它是否给你一个 json,如果没有,它会尝试另一个,如果没有,它会返回其他东西
  • 您在 JSON 中寻找的特定元素是否可以作为回报的依据?
  • 使用 if() 语句。
  • @BuzzotheSplitter 每个人都不同...

标签: php json parsing steam-web-api


【解决方案1】:

一些参考

$id = $_GET['id'];
$handlers =array(
    'getByJson','getByDB','getBySomething'
);
$result = array('handler'=>'','data'=>'');

然后调用 $handlers

foreach($handlers as $m){
    $methodResult = call_user_func($m,$id);
    if($methodResult !==false){
        $result = array('handler'=>$m, 'data' => $methodResult) ;
        break;
    }
}

处理类似的事情

function getByJson($id){
    $url = "http://steamcommunity.com/id/".$id."/inventory/json/730/2";
    $string = file_get_contents($url);
    $isJson =is_string($string) && is_object(json_decode($string)) && (json_last_error() == JSON_ERROR_NONE) ? true : false;
    if($isJson){
       $playerinfo = json_decode($content, true);
       return $playerinfo['success'];
    }

    return false;
}

function getByDB($id){
     // get $result

        if(condition){
            return $result
        }
        return false;

}

function getBySomething($id){
         // get $result

        if(condition){
            return $result
        }
        return false;
}

【讨论】:

    猜你喜欢
    • 2012-07-12
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多