将值从数据库传递到 API
Passing value from DB to API
我在这里要做的是,当我添加一个新用户时,$steamids
将获得该新值。但问题是我根本没有获得信息。似乎它没有从数据库中获取属性steam_id
。我能在这里做什么?
<?php
$dbh = new PDO('mysql:host=localhost;dbname=csgo', 'root', '');
$sql = "SELECT * FROM users";
$users = $dbh->query($sql);
foreach($users as $r) {
$steamids = $r['steam_id'];
$APIKEY = '*******************';
$steamAPI = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids=$steamids&key=$APIKEY&format=json";
$json_object = file_get_contents($steamAPI);
header('Content-Type: application/json');
echo $json_object;
}
当我只将普通 ID 添加到$steamids
时,它工作正常。但显然,我只能显示一个用户。我想显示多个,但我不知道我的代码出了什么问题。
好的!编辑
所以我这样做了:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=csgo', 'root', '');
$sth = $dbh->prepare("SELECT steam_id FROM users");
$sth->execute();
$result = $sth->fetchColumn();
$steamids = $result;
$APIKEY = '*******';
$steamAPI = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids=$steamids&key=$APIKEY&format=json";
$json_object= file_get_contents($steamAPI);
header('Content-Type: application/json');
echo $json_object;
现在我得到了steam_id,但只有第一行,我应该如何获得每个人的 ID?
试一试(使用fetch循环)
$dbh = new PDO('mysql:host=localhost;dbname=csgo', 'root', '');
$sth = $dbh->prepare("SELECT steam_id FROM users");
$sth->execute();
while($result = $sth->fetch())
{
$steamids = $result['steam_id'];
$APIKEY = '*******';
$steamAPI = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids=$steamids&key=$APIKEY&format=json";
$json_object= file_get_contents($steamAPI);
echo $json_object;
}
大概是这个?
<?php
error_reporting(-1); // Example where to add the PHP error reporting
ini_set('error_reporting', E_ALL); // if error reporting is switched off
header('Content-Type: application/json');
$dbh = new PDO('mysql:host=localhost;dbname=csgo', 'root', '');
$sth = $dbh->prepare("SELECT `steam_id` FROM `users`");
$APIKEY = '*******************';
$steamids = array();
if ($sth->execute()) {
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$steamids [] = $row["steam_id"];
$steamAPI = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?steamids=". $row["steam_id"] ."&key=". $APIKEY ."&format=json";
$json_object = file_get_contents($steamAPI);
echo $json_object;
sleep(1); // To pause the script for 1 second, OR try "usleep(500000);" for 0.5 second sleep, yes, "usleep".
}
}
echo "Test";
相关文章:
- 如何使用mysql数据库在node.js中登录API
- 从谷歌API地理代码的形式字段到数据库
- 从 Web SQL 数据库检索音频并使用 Web 音频 API 播放
- 通过API将视频信息插入数据库
- 访问MySQL数据库,使用JavaScript在Google Maps API V3上添加标记
- 通过服务器调用Graph API来构建我自己的Facebook帖子数据库
- 与 API 一起使用时,将 Date 对象存储在数据库中的最佳实践是什么
- 从 API 中提取信息并将其添加到数据库(平均堆栈)
- 将值从数据库传递到 API
- API 返回 404,但记录插入到数据库中
- 具有 REST API 和 ACL 的云中的数据库
- 使用谷歌地图API从本地数据库中搜索最近的地址
- Azure 自定义 API:执行数据库事务返回超时
- 创建具有 API 访问权限的机场数据库
- 使用 Google Maps JavaScript API 使用 SQL 数据库中的数据填充地图
- 将数据纬度/液化天然气从谷歌地图 API 保存到数据库中
- 为标记添加唯一的信息窗口,并更新数据库以存储信息(谷歌地图api)
- 商店定位器使用谷歌地图API和没有数据库
- 使用Express和Mongodb查询API和数据库
- 使用Web API从数据库中检索图像,然后将其显示在angularJS中