Download the PHP package adiafora/php-api-client without Composer
On this page you can find all versions of the php package adiafora/php-api-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adiafora/php-api-client
More information about adiafora/php-api-client
Files in adiafora/php-api-client
Package php-api-client
Short Description Packet to send to an Api using PHP.
License MIT
Informations about the package php-api-client
php-api-client
This package makes it very easy to access any API using both GET and POST requests. You can also send headers and parameters.
Installation
Run:
Configurations
You can set parameters for curl using global constants.
SET_CURLOPT_CONNECTTIMEOUT
- The number of seconds to wait while trying to connect. Use 0 to wait indefinitely. Default is 10.
SET_CURLOPT_TIMEOUT
- The maximum number of seconds to allow cURL functions to execute. Default is 10.
Usage
You must create a class that extends the Adiafora\ApiClient\AbstractApiClient
abstract class. And define the methods:
getHeaders()
- returns an array of headers.
getUrl()
- returns a string, the API url.
And in your project, you must refer to this class, which extends the Adiafora\ApiClient\AbstractApiClient
.
When accessing your class, you can pass the GET or POST request type (by default GET) and an array of request parameters, if any, to the constructor method. if you execute a GET request without parameters, you don't need to pass anything to the constructor method.
To make a request to the API, call the send()
method:
The method returns an object of class Adiafora\ApiClient\ApiResponse
that contains these methods:
code()
- return code of response.
response()
- return response from the API decoded using json_decode(). Because most APIs return a response in json format. If you want to get the answer as it is, then use the dirty() method.
dirty()
- return the answer as it is without transformations.
error()
- return curl_error() for the curl.
errno()
- return curl_errno() for the curl.
requestParameters()
- return parameters of request.
requestUrl()
- return url of request.
requestHeaders()
- return headers of request.
totalTime()
- return total transaction time in seconds for last transfer.
Real example
For example, we need to connect to the Yandex.Webmaster API.
For more information, see the API documentation https://yandex.com/dev/webmaster/doc/dg/concepts/About.html/
We are creating a class WebmasterApiClient
We have implemented 2 required methods getHeaders()
and getUrl()
. Everything else, including the implementation of the constructor method, is only necessary for implementing a specific API.
Now we want to get a list of all hosts. There is nothing easier! Just:
Or getting the history of SQI changes using the parameters:
License
The MIT License (MIT). Please see License File for more information.
All versions of php-api-client with dependencies
ext-curl Version *
ext-json Version *