dtdh11647 2015-05-11 20:45
浏览 174
已采纳

为foreach()提供的参数无效 - 无法遍历两个数组

My code should convert "defindex" from array_inv into "item_name" from array_schema:

<?php
    $apikey = "X";
    $steamid = $steamprofile['steamid'];
    $url_inv = "http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=". $apikey . "&steamid=" . $steamid . "&format=json";
    $url_schema = "http://git.optf2.com/schema-tracking/plain/Counter%20Strike%20Global%20Offensive%20Schema?h=counterstrikeglobaloffensive";
    $array_inv_raw = file_get_contents($url_inv);
    $array_schema_raw = file_get_contents($url_schema);

    $array_inv = json_decode($array_inv_raw,true);
    $array_schema = json_decode($array_schema_raw,true);

    foreach($array_inv['result']['items'] as $item){
        foreach($array_schema['result']['items'] as $schemaItem){
            if($item['defindex'] == $schemaItem['defindex']){
                echo $schemaItem['item_name'].'<br />';
                break;
            }
        }
    }
?>

But it's resulting in these errors:

Warning: file_get_contents(http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /home/kartm/public_html/scripts/inv.php on line 6

Warning: Invalid argument supplied for foreach() in /home/kartm/public_html/scripts/inv.php on line 12

The urls: array_inv array_schema

I can't find any mistakes. Can you tell me what's wrong with this code?

  • 写回答

1条回答 默认 最新

  • dougou1127 2018-09-16 22:31
    关注

    These errors were caused by a typo in a url:

    http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json
    

    It should have been & key=x instead of ?key=x.

    file_get_contents couldn't fetch the url, thus the foreach had an invalid, empty argument.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集