PHP Classes
elePHPant
Icontem

PHPLicengine API: Send HTTP requests to call REST Web service APIs

Recommend this page to a friend!
  Info   View files Documentation   View files View files (21)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-10-12 (4 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 4 This week: 4All time: 9,050 This week: 178Up
Version License PHP version Categories
phplicengine-api 1.0.0Custom (specified...5HTTP, PHP 5, Web services
Description Author

This class can send HTTP requests to call REST Web service APIs.

It can send HTTP requests to a given Web service API and decodes the response that may be returned in XML or JSON format.

The class uses Curl to send requests but it can customize options of the request to be sent to the server using a custom request sender callback function.

Recommendations

rest api class php 5.6
rest api php

Suitable out of the box REST API
REST API to replace service without a backend

REST API Structure for Web and mobile API
I want a PHP REST API Structure for Web and mobile development

Name: PHPLicengine <contact>
Classes: 1 package by
Country: Germany Germany

Details

API

PHPLicengine API

You can use this API library for any needs, not necessarily for PHPLicengine API. With PHPLicengine API library you can contact with any RESTApi server and receive response as json/xml and parse them.

Contents

Usage

You can use this API library for any needs, not necessarily for PHPLicengine API. To do so, you should call Api class directly or implement your own service class. You can call setApiKeyVar() method of Api class to change the Api key header variable according to requirements of your Api server, and setValidResponseHeader() method of Result class, if your Api server returns a response Api header, and you need to get it. You can get it with getReference() method. By default these are setup according to requirements of PHPLicengine API.

You can directly call Api class for your PHPLicengine API, but for your convenience we've created service classes that you can call them instead of Api class, for example see Client service class.

Installation

Versioning is the same as PHPLicengine. For general usage you can install any version as desired. But if you want to use PHPLicengine service classes, you should install the same version as your PHPLicengine or lower if there is not the same version.

composer require phplicengine/phplicengine-api x.x.x

Sample


use PHPLicengine\Api\Api;

$api = new Api();

// If your RESTApi server requires an Api key header, you can pass it to constructor of Api class:
// $api = new Api("API key goes here.");
// $api->setApiKeyVar("X-RESTAapi-Key");

// SSL verification is enabled by default. You can use below to disable it.
// $api->disableSslVerification();
// You can use below to enable it again.
// $api->enableSslVerification();

// timeout is set to 30 by default. You can use below to change it if needed.
// $api->setTimeout(60);

// Sets the host:port of a proxy to be used by cURL. If this is not set,  
// no proxy is used. For example, $api->setCurlProxy('proxy.example.com:3128');  
// $api->setCurlProxy($proxy);  

// get(), post(), delete(), put() methods are available.
// first parameter is url, second is query as array, third is header as array.
// Only first parameter (i.e. $url) is required.
$response = $api->get($url, null, null);

// For debug purposes only:
// print_r($api->getCurlInfo());
// print_r($response->getHeaders());
// print($response->getBody());
// exit;

// for logging purposes only:
// print($api->getResponse());
// print_r($api->getRequest());
// print_r($response->getHeaders());
// print($response->getBody());

if (!$api->isCurlError()) { // checks for cURL error

    if ($response->isOk()) { // checks for Code:200
    
        // If your RESTApi server returns a particular header as a valid response, 
        // you can set here:
        $response->setValidResponseHeader("X-ApiServer-Response");
        
        // This checks if the valid response header above is available or not.
        if ($response->isValidResponse()) {

            if ($response->isError()) { // if response of api has error
                print($response->getErrorMessage());
            } else {
                // $dataAsObject = $response->getDecodedJson();
                // echo $dataAsObject->username;
                // echo $response->getContentType();
                // You can get X-ApiServer-Response header value like below:
                // echo $response->getReference();
                print("<pre>");
                print_r($response->getJsonAsArray());
            }

        } else {
            print("Invalid server response.");
        }

    } else { // response code is not 200:Ok
        die("Error ".$response->getResponseCode()." : ".$response->getReasonPhrase());
    }
    
} else { // api curl Error occurs.
    die("Curl Connection: ".$api->getCurlErrno()." : ".$api->getCurlError());
}

NOTE: Usually RESTApi servers return a json response with 'error' and ' message' elements if an error occurs. If your RESTApi server returns another format, you'd need to customize isError() and getErrorMessage() methods in Result class.

Manual

Custom cURL Options

If you need to add some CURLOPT_* constants that are not enabled by default, you can call setCurlCallback() method to add them.

use PHPLicengine\Api\Api;
$api = new Api();
$api->setCurlCallback(function($ch, $params, $headers, $method) { 
      curl_setopt($ch, CURLOPT_USERAGENT, 'some agent'); 
      curl_setopt($ch, CURLOPT_COOKIE, 'foo=bar'); 
      curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
      curl_setopt($ch, CURLOPT_USERPWD, "username:password");
}); 
$api->post($url, $params, $headers);

Upload Files

You can upload files with POST method and with this array structure as post parameter. Note that 'filename' must be absolute path to file.

Array
(
    [files] => Array
        (
            [0] => Array
                (
                    [filename] => /path/to/file1.txt
                )
            [1] => Array
                (
                    [filename] => /path/to/file2.txt
                )
        )
    [foo] => bar
)

Service Classes

For service classes usage, See here.

Changelog

New methods: (v2.x.x)

// for logging purposes only:
print($api->getResponse());
print_r($api->getRequest());

// Sets the host:port of a proxy to be used by cURL. If this is not set,  
// no proxy is used. For example, $api->setCurlProxy('proxy.example.com:3128');  
$api->setCurlProxy($proxy); 

License

PHPLicengine Api is distributed under the Apache License. See License.

  Files folder image Files  
File Role Description
Files folder imageexamples (6 files)
Files folder imagelib (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file client.md Doc. Documentation
  Accessible without login Plain text file license.md Lic. License text
  Accessible without login Plain text file order.md Data Auxiliary data
  Accessible without login Plain text file product.md Data Auxiliary data
  Accessible without login Plain text file README.md Doc. Documentation
  Accessible without login Plain text file reference.md Data Auxiliary data

  Files folder image Files  /  lib  
File Role Description
Files folder imagePHPLicengine (3 directories)

  Files folder image Files  /  lib  /  PHPLicengine  
File Role Description
Files folder imageApi (3 files)
Files folder imageException (4 files)
Files folder imageService (5 files)

  Files folder image Files  /  lib  /  PHPLicengine  /  Api  
File Role Description
  Plain text file Api.php Class Class source
  Plain text file FileUpload.php Class Class source
  Plain text file Result.php Class Class source

  Files folder image Files  /  lib  /  PHPLicengine  /  Exception  
File Role Description
  Plain text file CoreException.php Class Class source
  Plain text file CurlException.php Class Class source
  Plain text file FileException.php Class Class source
  Plain text file ResponseException.php Class Class source

  Files folder image Files  /  lib  /  PHPLicengine  /  Service  
File Role Description
  Plain text file Client.php Class Class source
  Plain text file License.php Class Class source
  Plain text file Order.php Class Class source
  Plain text file Product.php Class Class source
  Plain text file Reference.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:4
This week:4
All time:9,050
This week:178Up