dq1230123 2013-06-15 11:51
浏览 39

混合2个cURL请求

This first snippet retrieves data from an API, json_decode(s) it and prints some data I choose:

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

}

?>

This other snippet authenticates with a key and send the command to update the data in the APIs:

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

?>

How can I mix these 2 functions so I have: First updates the API (second function), then retrieves $data and with the foreach I extrapolate entries from JSON.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向