qhayat/php-mondialrelay-webservice

PHP 对 Mondial Relay Web Service 的接口。

1.0 2024-09-18 18:23 UTC

This package is auto-updated.

Last update: 2024-09-18 18:28:01 UTC


README

一个表达性丰富的流畅接口,用于Mondial Relay的物流服务。

安装

使用 Composer 安装

$ composer require cba85/php-mondialrelay-webservice

注意:您必须启用PHP的Soap功能。

入门指南

<?php

require __DIR__ . '/vendor/autoload.php';

// 1. Initialize Mondial Relay webservice with your credentials.
$mondialrelay = new Webservice('BDTEST13', 'PrivateK');

// 2. Create parameters for the method you want to use.
 $parameters = [
    'Pays' => 'FR',
    'Ville' => 'Paris',
    'NbResult' => 5
];

// 3. Call the method using your parameters and get the results in json format
$searchPostcode = $mondialrelay->searchPostcode($parameters)->getResultsInJson();

// {"STAT":"0","Liste":{"Commune":[{"CP":"75001","Ville":"PARIS","Pays":"FR"},{"CP":"75002","Ville":"PARIS","Pays":"FR"},{"CP":"75003","Ville":"PARIS","Pays":"FR"},{"CP":"75004","Ville":"PARIS","Pays":"FR"},{"CP":"75005","Ville":"PARIS","Pays":"FR"}]}}

用法

Web服务

您必须获取Mondial Relay的凭据才能使用Web服务。

您可以使用Mondial Relay的测试凭据来尝试Web服务

  • 商家(商店):BDTEST13
  • 私钥:PrivateK

可用方法

结果

您可以将Web服务的结果作为PHP StdClass对象或Json格式检索。

$mondialrelay = new Webservice('BDTEST13', 'PrivateK');

// E.g. Call a method
$parameters = [...]; // Parameters of the method

// PHP StdClass object results
$searchParcelshop = $mondialrelay->searchParcelshop($parameters)->getResults();

// Json results
$searchParcelshop = $mondialrelay->searchParcelshop($parameters)->getResultsInJson();

上次方法调用

您可以使用包含上次方法调用的method属性检索结果、参数或调用另一个Web服务请求。

$mondialrelay = new Webservice('BDTEST13', 'PrivateK');

// E.g. Call a method
$parameters = [...]; // Parameters of the method

$searchParcelshop = $mondialrelay->searchParcelshop($parameters)->getResults();

$mondialrelay->method->parameters; // Get last method called parameters
$mondialrelay->method->results; // Get last method called results
$mondialrelay->method->request()->results(); // Create another request using last method and parameters called and get the results

参数

您通过方法传递的参数将根据Mondial Relay的正则表达式模式自动检查。

邮政编码参数和电话号码参数将根据它们所依赖的国家参数自动检查。

获取设置参数

您可以使用以下方式检查您调用的最后一个方法的设置参数

$mondialrelay = new Webservice('BDTEST13', 'PrivateK');

// E.g. Call a method
$parameters = [...]; // Parameters of the method
$searchParcelshop = $mondialrelay->searchParcelshop($parameters)->getResults();

print_r($mondialrelay->method->parameters);

获取错误参数

如果在参数检查过程中至少有一个参数不符合其正则表达式模式,将抛出异常以防止无用的Web服务调用。

您可以检索这些错误

$mondialrelay = new Webservice('BDTEST13', 'PrivateK');

// E.g. Call a method
$parameters = ['Pays' => "FRA"]; // Parameters of the method with at least a bad parameter

// Display parameters with error
try {
    $searchParcelshop = $mondialrelay->searchParcelshop($parameters)->getResults();
} catch (Exception $e) {
    print_r($mondialrelay->method->parameter->getErrors());
}

/*
Array
(
    [Pays] => FRA
)
*/

文档

测试

$ ./vendor/bin/phpunit --bootstrap vendor/autoload.php

如果您想测试特定内容,只需添加您要测试的文件名。

示例

$ ./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/WebserviceTest.php