PHP code example of wandesnet / mercado-livre

1. Go to this page and download the library: Download wandesnet/mercado-livre library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wandesnet / mercado-livre example snippets


final class Meli extends MeliConnector
{

    public ?int $tries = 2; // default 1, try to request api 2 times if fail

    public function resolveBaseUrl(): string
    {
        return 'https://api.mercadolibre.com';
    }

    protected function defaultOauthConfig(): OAuthConfig
    {
        return OAuthConfig::make()
            ->setClientId('3232321')
            ->setClientSecret('fs4343fs423')
            ->setAuthorizeEndpoint('https://auth.mercadolivre.com.br/authorization')
            ->setRedirectUri('http://my-site.com/callback.php')
            ->setTokenEndpoint('oauth/token');
    }

    protected function refreshTokenExpireIn(): void
    {
        if ($this->hasExpired()) {
            $refresh = $this->refreshAccessToken($this->refresh_token);

            $this->access_token = $refresh->getAccessToken();
            $this->refresh_token = $refresh->getRefreshToken();
            $this->expires_in = $refresh->getExpiresAt()->getTimestamp();
        }
    }
}

$response = Meli::make('access_token', 'refresh_token', 'expire_in')->auth()->request()->order(123456789);

 Meli::make()->getAuthorizationUrl();

 $authenticator = Meli::make()->getAccessToken($_GET['code']);

var_dump($authenticator->getAccessToken(), $authenticator->getRefreshToken(), $authenticator->getExpiresAt()->getTimestamp());