将值从DB传递给API

时间:2016-02-25 09:33:46

标签: javascript php

我想在这里做的是当我添加一个新用户时,$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;
    }

当我向$steamids添加纯文本ID时,工作正常。但显然我只能显示一个用户。我想显示多个,我不知道我的代码有什么问题。

确定! 编辑

所以我这样做了:

<?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但只有第一行,我应该如何获得每个人的身份证?

2 个答案:

答案 0 :(得分:0)

可能这个?

<?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";

答案 1 :(得分:0)

给这一点(通过使用获取循环)

vertical spaces