PHP code example of zdearo / meli-php

1. Go to this page and download the library: Download zdearo/meli-php 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/ */

    

zdearo / meli-php example snippets


use Zdearo\Meli\Facades\Meli;

// Get a product — returns Product DTO
$product = Meli::products()->get('MLB123456789');
$product->title;       // string
$product->price;       // float
$product->status;      // string

// Search products — returns SearchResult DTO
$results = Meli::searchItem()->byQuery('smartphone samsung');
$results->results;     // array
$results->total;       // int

// Get user — returns User DTO
$user = Meli::users()->me();
$user->nickname;       // string

// Get orders — returns SearchResult with Order DTOs
$orders = Meli::orders()->getBySeller(123456789);
$orders->results;      // array<Order>

use Zdearo\Meli\Facades\Meli;

// Generate authorization URL
$authUrl = Meli::getAuthUrl('your-state-parameter');

// Exchange code for token — returns AuthToken DTO
$token = Meli::auth()->getToken($code);
$token->accessToken;    // string
$token->refreshToken;   // string
$token->expiresIn;      // int

// Refresh token
$newToken = Meli::auth()->refreshToken($token->refreshToken);

// Per-request token
$user = Meli::withToken($accessToken)->users()->me();

// Via connection object
$orders = Meli::forConnection($meliConnection)->orders()->getBySeller($sellerId);

// Via resolver in config
config(['meli.access_token_resolver' => function ($connectionId = null) {
    if ($connectionId) {
        return MeliConnection::find($connectionId)?->access_token;
    }
    return auth()->user()?->meli_access_token;
}]);

// Create
$product = Meli::products()->create([
    'title' => 'Amazing Product',
    'category_id' => 'MLB1055',
    'price' => 99.99,
    'currency_id' => 'BRL',
    'available_quantity' => 10,
    'condition' => 'new',
]);

// Update
$product = Meli::products()->update('MLB123456789', [
    'price' => 89.99,
    'available_quantity' => 5,
]);

// Change status
$product = Meli::products()->changeStatus('MLB123456789', 'paused');

use Zdearo\Meli\Exceptions\MeliNotFoundException;
use Zdearo\Meli\Exceptions\MeliValidationException;
use Zdearo\Meli\Exceptions\MeliRateLimitException;
use Zdearo\Meli\Exceptions\MeliException;

try {
    $product = Meli::products()->get('MLB123456789');
} catch (MeliNotFoundException $e) {
    // 404 — resource not found
    $e->statusCode;  // 404
    $e->errorCode;   // "not_found"
    $e->context;     // full response body as array
} catch (MeliValidationException $e) {
    // 400 — validation errors
    $e->causes;      // array of validation errors from ML
} catch (MeliRateLimitException $e) {
    // 429 — too many requests
} catch (MeliException $e) {
    // Catch-all for any API error
    Log::error("Meli [{$e->statusCode}]: {$e->getMessage()}", $e->context);
}

// Memory-efficient — fetches pages on demand
Meli::orders()
    ->searchAll(['seller' => $sellerId])
    ->cursor()
    ->each(function (\Zdearo\Meli\DTO\Order $order) {
        // Process each order
    });

// Collect all into a Collection
$allOrders = Meli::orders()->searchAll(['seller' => $sellerId])->all();

use Zdearo\Meli\Facades\Meli;
use Zdearo\Meli\Exceptions\MeliNotFoundException;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function show(string $itemId)
    {
        try {
            $product = Meli::products()->get($itemId);
            return view('products.show', compact('product'));
        } catch (MeliNotFoundException) {
            abort(404);
        }
    }

    public function search(Request $request)
    {
        $results = Meli::searchItem()->byQuery($request->input('q'));
        return view('products.search', ['results' => $results->results, 'total' => $results->total]);
    }

    public function orders()
    {
        $orders = Meli::orders()->getBySeller(auth()->user()->meli_user_id);
        return view('orders.index', ['orders' => $orders->results]);
    }
}

$results = Meli::documentation()->search('items', 'pt_BR');
$page = Meli::documentation()->getPage('/pt_br/product-search', 'pt_BR');
bash
composer 
bash
php artisan vendor:publish --tag=meli-config