为 foreach() 提供的参数无效 - 无法遍历两个数组
Invalid argument supplied for foreach() - can't loop through two arrays
我的代码应该将 "defindex" 从 array_inv 转换为 "item_name" 从 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;
}
}
}
?>
但它导致了这些错误:
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
我找不到任何错误。你能告诉我这段代码有什么问题吗?
这些错误是由 url:
中的拼写错误引起的
http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json
应该是 & key=x 而不是 ?key=x。
file_get_contents 无法获取 url,因此 foreach 有一个无效的空参数。
我的代码应该将 "defindex" 从 array_inv 转换为 "item_name" 从 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;
}
}
}
?>
但它导致了这些错误:
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
我找不到任何错误。你能告诉我这段代码有什么问题吗?
这些错误是由 url:
中的拼写错误引起的http://api.steampowered.com/IEconItems_730/GetPlayerItems/v0001/?language=en?key=x&steamid=76561198037897388&format=json
应该是 & key=x 而不是 ?key=x。
file_get_contents 无法获取 url,因此 foreach 有一个无效的空参数。