climba-commerce/mercadolibre-php-sdk

这是生成Open Platform Marketplace SDK的代码库

v3.2.3 2023-08-08 22:18 UTC

This package is not auto-updated.

Last update: 2024-09-18 03:22:30 UTC


README

No longer maintained

MercadoLibre的PHP SDK

这是MercadoLibre平台官方的PHP SDK。

需求

PHP 8.0及更高版本

安装与使用

Composer

要通过Composer安装绑定,请将以下内容添加到composer.json

{
  "require": {
    "climba-commerce/mercadolibre-php-sdk": "^v3.1"
  }
}

然后运行composer install

手动安装

下载文件

运行composer install

在您的代码中包含autoload.php

    require_once('/path-to-integration-folder/vendor/autoload.php');

测试

要运行单元测试

composer install
./vendor/bin/phpunit

用法

<?php
require_once(__DIR__ . '/vendor/autoload.php');

$config = new Meli\Configuration();
$servers = $config->getHostSettings();
// Auth URLs Options by country

// 1:  "https://auth.mercadolibre.com.ar"
// 2:  "https://auth.mercadolivre.com.br"
// 3:  "https://auth.mercadolibre.com.co"
// 4:  "https://auth.mercadolibre.com.mx"
// 5:  "https://auth.mercadolibre.com.uy"
// 6:  "https://auth.mercadolibre.cl"
// 7:  "https://auth.mercadolibre.com.cr"
// 8:  "https://auth.mercadolibre.com.ec"
// 9:  "https://auth.mercadolibre.com.ve"
// 10: "https://auth.mercadolibre.com.pa"
// 11: "https://auth.mercadolibre.com.pe"
// 12: "https://auth.mercadolibre.com.do"
// 13: "https://auth.mercadolibre.com.bo"
// 14: "https://auth.mercadolibre.com.py"

// Use the correct auth URL
$config->setHost($servers[1]["url"]);

// Or Print all URLs
print_r($servers);

// Or Print or Put the following URL in your browser window to obtain authorization:
//
// http://auth.mercadolibre.com.ar/authorization?response_type=code&client_id=$APP_ID&redirect_uri=$YOUR_URL
?>

这将提供用户重定向的URL。您需要指定一个回调URL,用户在成功授权过程后将重定向到该URL。

用户被重定向到您的回调URL后,您将在查询字符串中收到一个名为code的参数。您需要此参数用于过程的第二部分

OAuth示例 - 获取令牌

<?php
require_once(__DIR__ . '/vendor/autoload.php');


$apiInstance = new Meli\Api\OAuth20Api(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client()
);
$grant_type = 'authorization_code';
$client_id = 'client_id_example'; // Your client_id
$client_secret = 'client_secret_example'; // Your client_secret
$redirect_uri = 'redirect_uri_example'; // Your redirect_uri
$code = 'code_example'; // The parameter CODE
$refresh_token = 'refresh_token_example'; // Your refresh_token

try {
    $result = $apiInstance->getToken($grant_type, $client_id, $client_secret, $redirect_uri, $code, $refresh_token);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling OAuth20Api->getToken: ', $e->getMessage(), PHP_EOL;
}
?>

使用RestClient通过POST项的示例

<?php
require_once(__DIR__ . '/vendor/autoload.php');


$apiInstance = new Meli\Api\RestClientApi(
    // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
    // This is optional, `GuzzleHttp\Client` will be used as default.
    new GuzzleHttp\Client()
);
$resource = 'resource_example'; // string | for example: items
$access_token = 'access_token_example'; // string |
$options = ['headers' => ['access_token' => $access_token]]; // array |
$body = new \stdClass; // object |

try {
    $apiInstance->resourcePost($resource, $options, $body);
} catch (Exception $e) {
    echo 'Exception when calling RestClientApi->resourcePost: ', $e->getMessage(), PHP_EOL;
}
?>

文档和重要注意事项

URI相对于https://api.mercadolibre.com
授权URL(设置正确的国家域名):https://auth.mercadolibre.{country_domain}
库的所有文档都位于这里
查看我们在文件夹examples中的示例代码
别忘了查看我们的开发者网站