0

この最初のスニペットは、API からデータを取得し、それを json_decode(s) して、選択したデータを出力します。

<?php
$data=array('players'=>array(),'opt'=>array());

// List of players
$data['players'][]='multiforme';

// List of options
$data['opt']['clear']=true;
$data['opt']['global']=true;
$data['opt']['rank']=true;
$data['opt']['scores']=true;
$data['opt']['imgInfo']=true;
$data['opt']['urls']=true;
$data['opt']['lastseen']=true;

// Platform can be pc, 360, ps3
$platform='pc';

// Convert lists to JSON
$postdata=array();
$postdata['players']=json_encode($data['players']);
$postdata['opt']=json_encode($data['opt']);

// Run POST Request via CURL
$c=curl_init('http://api.bf3stats.com/'.$platform.'/playerlist/');
curl_setopt($c,CURLOPT_HEADER,false);
curl_setopt($c,CURLOPT_POST,true);
curl_setopt($c,CURLOPT_USERAGENT,'BF3StatsAPI/0.1');
curl_setopt($c,CURLOPT_HTTPHEADER,array('Expect:'));
curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
curl_setopt($c,CURLOPT_POSTFIELDS,$postdata);
$data=curl_exec($c);
$statuscode=curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);

if($statuscode==200) {
    // Decode JSON Data
    $data=json_decode($data,true);

    // Print data for debugging
    echo '<pre>';

// print list of players with rank and score
    foreach($data['list'] as $d) {
        $stats=$d['stats'];
        echo '<a href="http://battlelog.battlefield.com/bf3/soldier/multiforme/stats/883167655/">' . $d['name'].'</a>'." | Livello <img src=\"http://www.teamkillerhunters.com/bf3stats/stylesheets/images/".$stats['rank']['img_tiny']."\" title=\"".$stats['rank']['nr']."\" alt=\"".$stats['rank']['nr']."\"/> | Punteggio: ".$stats['scores']['score']. " | Ultima partita: ".htmlentities(strftime('%e %B %Y alle %R', $d['date_lastseen']));
    }
    echo '</pre>';
    // ... now you use $data to do whatever

} else {
    echo "BF3 Stats API error status: ".$statuscode;

}

?>

この別のスニペットはキーで認証し、コマンドを送信して API のデータを更新します。

<?php

// API access data
$BF3APIIdent='multiforme';
$BF3APIKey='XXXXXXXX';

// Platform can be pc, 360, ps3
$platform='pc';


$data=updatePlayer($platform,'multiforme');
#$data=lookupPlayer($platform,'multiforme');

#$data=setupClientKey('testident','For testing');
#$data=getClientKey('R2DGATJ9ek');


// Print data for debugging
echo '<pre>';
print_r($data);
echo '</pre>';


function setupClientKey($clientident=null,$name=null) {
    global $BF3APIIdent;
    $data=array();
    $data['ident']=$BF3APIIdent;
    $data['time']=time();
    $data['clientident']=$clientident;
    $data['name']=$name;
    return getAPIDataSigned('setupkey','global',$data);
}

function getClientKey($clientident) {
    global $BF3APIIdent;
    $data=array();
    $data['ident']=$BF3APIIdent;
    $data['time']=time();
    $data['clientident']=$clientident;
    return getAPIDataSigned('getkey','global',$data);
}

function lookupPlayer($platform,$name) {
    global $BF3APIIdent;
    $data=array();
    $data['ident']=$BF3APIIdent;
    $data['time']=time();
    $data['player']=$name;
    return getAPIDataSigned('playerlookup',$platform,$data);
}


function updatePlayer($platform,$name) {
    global $BF3APIIdent;
    $data=array();
    $data['ident']=$BF3APIIdent;
    $data['time']=time();
    $data['player']=$name;
    return getAPIDataSigned('playerupdate',$platform,$data);
}


function getAPIDataSigned($type,$platform,$data) {
    global $BF3APIKey;
    // Convert data to JSON
    $urlbase64=array('+'=>'-','/'=>'_','='=>'');
    $data=strtr(base64_encode(json_encode($data)),$urlbase64);

    $postdata=array();
    $postdata['data']=$data;
    $postdata['sig']=strtr(base64_encode(hash_hmac('sha256',$data,$BF3APIKey,true)),$urlbase64);

    // Run POST Request via CURL
    $c=curl_init('http://api.bf3stats.com/'.$platform.'/'.$type.'/');
    curl_setopt($c,CURLOPT_HEADER,false);
    curl_setopt($c,CURLOPT_POST,true);
    curl_setopt($c,CURLOPT_USERAGENT,'BF3StatsAPI/0.1');
    curl_setopt($c,CURLOPT_HTTPHEADER,array('Expect:'));
    curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($c,CURLOPT_POSTFIELDS,$postdata);
    $data=curl_exec($c);
    $statuscode=curl_getinfo($c,CURLINFO_HTTP_CODE);
    curl_close($c);

    if($statuscode==200) {
        // Decode JSON Data
        $data=json_decode($data,true);
        return $data;

    } else {
        echo "BF3 Stats API error status: ".$statuscode;
        return false;
    }
}

?>

これら 2 つの関数を混在させるにはどうすればよいでしょうか: 最初に API を更新し (2 番目の関数)、次に $data を取得し、foreach を使用して JSON からエントリを推定します。

4

0 に答える 0