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;
    }
}

?>

我怎样才能混合这两个函数,所以我有:首先更新 API(第二个函数),然后检索 $data 并使用 foreach 从 JSON 中推断条目。

4

0 回答 0