Download the PHP package pdeans/miva-api without Composer
On this page you can find all versions of the php package pdeans/miva-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package miva-api
Miva JSON API PHP Library
PHP library for interacting with the Miva JSON API.
Table Of Contents
- Installation
- Configuring the API Client
- Client Configuration Options
- Authentication
- JSON Request Format
- Function Builder
- Function Request Parameters
- Common Filter List Parameters
- Function Request Filters
- Search
- On Demand Columns
- Show
- Function Request Input Parameters
- Passphrase
- Additional Input Parameters
- Function Request Parameters
- API Requests
- HTTP Headers
- Sending Requests
- API Responses
- Checking For Request Errors
- Response Body
- Helpers
- Troubleshooting API Requests And Responses
- Further Reading
- Testing
Installation
Requirements:
- PHP 8.3+
- ext-json
Install via Composer.
Configuring the API Client
Utilizing the library to interact with the API is accomplished via the Client class. The Client class accepts an array containing API and HTTP client (Guzzle) configuration options in key/value format.
Client Configuration Options
| Key | Required | Type | Description |
|---|---|---|---|
| url | Yes | string | The API endpoint URL. |
| store_code | Yes | string | The Miva store code. |
| access_token | Yes | string | The API access token. |
| private_key | Yes | string | The API signature key. Hint: If omitting signature validation, pass in an '' empty string literal value. |
| hmac | No | string | HMAC signature type. Defaults to sha256. Valid types are sha256, sha1, or '' (blank string literal to disable HMAC; header becomes MIVA <token>). |
| timestamp | No | boolean | Enable/disable API request timestamp validation. Defaults to true (Enabled). |
| timeout | No | int | Override default 60s Miva request timeout. Sends X-Miva-API-Timeout header. |
| binary_encoding | No | string | json (default) or base64. Sends X-Miva-API-Binary-Encoding header when not json. |
| range | No | string | Range header value for multi-call retries (e.g., Operations=4-5). |
| ssh_auth | No | array | SSH authentication (takes precedence over token authentication when present): ['username' => string, 'private_key' => string, 'algorithm' => 'sha256'\|'sha512']. private_key may be a file path (preferred) or raw PEM contents. Supported private key formats: PKCS#1 PEM or PKCS#8 PEM (convert OpenSSH keys before use). |
| http_headers | No | array | HTTP request headers to merge. The library automatically sets the following default headers for every request: Content-Type, Accept, User-Agent, and the appropriate auth header. These headers should not be included in this list. |
| http_client | No | array | Associative array of Guzzle request options, or an instance of GuzzleHttp\ClientInterface. |
Example:
Authentication
The Miva API authorization header is generated based on the configuration:
- Token authentication (default):
X-Miva-API-Authorizationis built asMIVA-HMAC-SHA256 <token>:<signature>(orSHA1). Ifhmacorprivate_keyis an empty string, the header becomesMIVA <token>with no HMAC signature. - SSH authentication:
X-Miva-API-Authorizationis built asSSH-RSA-SHA2-256|SHA2-512 <base64(username)>:<base64(signature)>. Whenssh_authis provided, SSH auth is used and supersedes token auth. Supported key formats are PKCS#1 PEM and PKCS#8 PEM; convert OpenSSH keys to PEM before use.
JSON Request Format
The required Miva_Request_Timestamp and Store_Code properties are automatically generated based on the configuration settings passed into the Client object and added to the JSON body for every API request. The Function property is also automatically added to the JSON body for every request. The JSON data generated for the Function property will vary based on the provided request function list.
Function Builder
The func method is used to generate API request functions. The method accepts the request function name as its only argument.
The add method is used to "publish" the function and append it to the request function list.
Note: All function builder methods are chainable.
Function Request Parameters
This section showcases how to construct and add function parameters.
Common Filter List Parameters
Each common filter list parameter for the xxxList_Load_Query functions has a corresponding helper method to seamlessly set the parameter value. The example below shows each of the methods in action.
Function Request Filters
Most of the function search/display filters have an associated helper method that acts as a shorthand, or factory for creating the respective filter. The filter method must be used for any filter that does not have a linked helper method, as shown in the example above. This method can also be used to create each filter covered below. The method accepts two arguments, with the first argument always being the filter name. The second argument takes the filter value, which will vary per filter type.
The available search/display helper methods are covered below.
Search
The search method may be used to attach a search filter to a function's filter list. The most basic call to search requires three arguments. The first argument is the search field column. The second argument is the search operator, which can be any of the supported API search operators. Finally, the third argument is the value to evaluate against the search column.
Below is an example to issue a search filter for a specific product code:
For convenience, if you want to verify that a column is equal ('EQ') to a given value, you may pass the value directly as the second argument to the search method. The following will achieve the same result as the first example above:
Of course, you may use a variety of other supported operators when writing a search filter:
The search method can be issued multiple times to perform an AND search:
Performing OR searches and parenthetical comparisons can be achieved by passing in an array to the search method as the first and only argument. The array should be modeled to match the desired search value output, with value nesting as needed:
On Demand Columns
Using the ondemandcolumns method, you can specify the explicit columns to return. The method takes one argument, the list of on demand columns to select:
For convenience, the odc method can be utilized as an alias to the ondemandcolumns method:
Show
The "show" filters can be created using the show method. This method takes one argument, the show filter value, which will vary per xxxList_Load_Query function. Note that this filter is currently available for the following functions only:
- CategoryList_Load_Query
- CategoryProductList_Load_Query
- ProductList_Load_Query
Example:
Function Request Input Parameters
Passphrase
The passphrase method is used to set the Passphrase parameter. The method takes a single argument, the decryption passphrase.
Additional Input Parameters
The params method is used to set all other input parameters. Some example use cases for this method are the request body parameters for the Xx_Create / Xx_Insert / Xx_Update / Xx_Delete functions, Module level functions, and essentially all other functions that require specific input parameters to perform actions. The function accepts a key/value array which maps to the input parameter key/values as its only argument.
Examples:
API Requests
This section covers configuring and issuing API requests.
HTTP Headers
You may specify which HTTP headers are attached to all API requests with the addHeader and addHeaders methods. By default the library sets:
Content-Type: application/jsonAccept: application/jsonUser-Agent: MVPSMivaApi/<version> (php/<php_version> <php_os>)X-Miva-API-Authorization, automatically formatted for token or SSH authentication based on your configuration.- Optional headers when configured:
X-Miva-API-Timeout,X-Miva-API-Binary-Encoding, andRange.
Sending Requests
The send method will issue an API request, and return the results in the library's Response object. If you wish to bypass this object and return the raw JSON response from the API, pass a true value as the first argument for the send method.
Example requests:
You may preview the current request body at any time before sending the request by using the getRequestBody method. This is helpful for debugging requests.
API Responses
By default, API responses will return a pdeans\Miva\Api\Response class instance. The Response object includes a number of helper methods for interacting with the API responses.
Checking For Request Errors
Use hasErrors() to check for any errors across functions/iterations/operations. The errors() method returns an ErrorBag with helpers such as all(), messages(), and forField('Field_Name'). successful() indicates if any result succeeded; failed() is the inverse. Mixed success/failure responses (e.g., operations with some errors) will still report successful() true but hasErrors() true.
Response Body
The raw JSON response body can be retrieved anytime using the getBody method:
To receive an iterable form of the API response, issue the getResponse method. This will return an array of objects, with the array keys mapping to the function names supplied to the API request function list. The items are sorted in identical order to the API request function list. Each item or "function", contains its own array of the results of the function request. These array items correlate to each of the function's iterations that were sent in the request. The items are sorted in the same order that they were issued in the request. Use the getFunctions method to retrieve the list of available functions.
The getFunction method may be used to explicitly return the response results for a specific function name. This can also be accomplished with the getResponse method by passing the function name as the first argument.
The getData method returns the response data property for a specific function name. By default, the data property is returned for the first iteration index for the function name provided. However, an optional second argument can be provided to return the data property for a specific iteration index on the given function name.
Examples:
Helpers
This section covers library helper methods.
Troubleshooting API Requests And Responses
To aid in troubleshooting API requests and responses, PSR-7 Request and PSR-7 Response objects can be obtained using the getPreviousRequest and getPreviousResponse methods respectively after an API request has been issued:
Furthermore, the getUrl, getHeaders, and getFunctionList methods may be used to inspect and troubleshoot requests before they are sent off to the API:
Further Reading
Having a general understanding of the Miva JSON API configuration and schema is highly recommended before using the library.
Testing
Install dev dependencies:
Copy the example environment file and provide valid credentials for a test store:
Populate the following variables in .env (values are not committed):
MIVA_API_URLMIVA_API_STORE_CODEMIVA_API_ACCESS_TOKENMIVA_API_PRIVATE_KEYMIVA_API_HTTP_AUTHMIVA_API_HTTP_CACHEMIVA_API_HTTP_VERIFY(set tofalsefor dev certificates)MIVA_API_HTTP_HEADERS(optional JSON map of extra headers)
Optional for SSH auth testing:
MIVA_API_SSH_USERNAMEMIVA_API_SSH_PRIVATE_KEY_PATHMIVA_API_SSH_ALGORITHMMIVA_API_SSH_LIVE
Run the test suite:
Static analysis and style checks: