在具有API的网站上,我有5个帐户。我想做一个带来总价值的项目。
我想通过插入所有用户,发出API请求,在txt文件中获取和写入令牌来创建php类。但这不是最好的选择。
我正在寻找解决问题的最佳方法。
<?php
session_start();
require '../Meli/meli.php';
require '../configApp.php';
$meli = new Meli($appId, $secretKey);
if(isset($_GET['code']) || isset($_SESSION['access_token'])) {
// If code exist and session is empty
if(isset($_GET['code']) && !isset($_SESSION['access_token'])) {
// //If the code was in get parameter we authorize
try{
$user = $meli->authorize($_GET["code"], $redirectURI);
// Now we create the sessions with the authenticated user
$_SESSION['access_token'] = $user['body']->access_token;
$_SESSION['expires_in'] = time() + $user['body']->expires_in;
$_SESSION['refresh_token'] = $user['body']->refresh_token;
}catch(Exception $e){
echo "Exception: ", $e->getMessage(), "\n";
}
} else {
// We can check if the access token in invalid checking the time
if($_SESSION['expires_in'] < time()) {
try {
// Make the refresh process
$refresh = $meli->refreshAccessToken();
// Now we create the sessions with the new parameters
$_SESSION['access_token'] = $refresh['body']->access_token;
$_SESSION['expires_in'] = time() + $refresh['body']->expires_in;
$_SESSION['refresh_token'] = $refresh['body']->refresh_token;
} catch (Exception $e) {
echo "Exception: ", $e->getMessage(), "\n";
}
}
}
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
} else {
echo '<a href="' . $meli->getAuthUrl($redirectURI, Meli::$AUTH_URL[$siteId]) . '">Login using MercadoLibre oAuth 2.0</a>';
}