PHP code example of mslib / remote-host

1. Go to this page and download the library: Download mslib/remote-host library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mslib / remote-host example snippets

 bash
$ php composer.phar update mslib/remote-host
 php


return array(
    'parameters' => array(
        'host'      => 'http://www.example.com/api/', 
    ),
    'actions_parameters' => array(
    ),
    'config' => array(
        'maxredirects'  => 2,
        'timeout'       => 30,
        'adapter'       => 'Zend\Http\Client\Adapter\Curl',
    ),
    'actions' => array(
        'example-api' => array( 
            'script-list' => array( 
                'name'              => 'scripts',
                'adds-on'           => array(
                    array(
                        'type'    => 'plain',
                        'content' => 'scripts'
                    )
                ),
                'request'           => array(
                    'type'                     => 'UrlEncoded', 
                    'parameters'               => array( 
                        'format'      => 'json', 
                    ),
                ),
                'response' => array(
                    'type'      => 'Json', 
                ),
            ),
        ),
    ),
);
 php


return array(
    'parameters' => array(
        'host'      => '', // (REQUIRED) The host api
        'port'      => '', // (optional) The port
        'user'      => '', // (optional) The user
        'password'  => '', // (optional) The password
    ),
    'actions_parameters' => array(
        // Add here all the parameters which are common to all actions. You still can override them by adding a different value in the parameters array of each action
        'key1' => 'value1',
        ...
    ),
    // For all possible values, please look at Zend\Http\Client->$config
    'config' => array(
        'adapter'       => 'Zend\Http\Client\Adapter\Curl', // Please, leave this value for stability reason
        ...
    ),
    'actions' => array(
        'action-group' => array( // (REQUIRED) First Level Action Name
            'action-name-1' => array( // (REQUIRED) Second Level Action Name
                ...
            ),
            ...
            ...
            'action-name-N' => array( // (REQUIRED) Second Level Action Name
                ...
            ),
        ),

    ),
);
 php


return array(
    'parameters' => array(
        'host'      => 'http://www.example.com/api',
        'port'      => '80',
        'user'      => 'testUser',
        'password'  => 'testPassword',
    ),
    ...
    ...
    ...
);
 php


return array(
    'parameters' => array(
        'host'      => 'http://www.example.com/api',
        'port'      => '80',
        'user'      => 'testUser',
        'password'  => 'testPassword',
    ),
    'actions_parameters' => array(
        'param1'    => 'default-value-1',
        'param2'    => 'default-value-2',
        ...
        ...
    )
    ...
    ...
);
 php


return array(
    ...
    ...
    'config' => array(
        'maxredirects'  => 2,
        'timeout'       => 30,
        'adapter'       => 'Zend\Http\Client\Adapter\Curl',
        ...
        ...
    )
    ...
    ...
);
 php


return array(
    'parameters' => array(
        'host'      => 'http://www.example.com/api',
        ...
        ...
    ),
    ...
    ...
    'actions' => array(
        'action-group-1' => array(
            'action-name-1' => array(
                ...
                ...
                'request'           => array(
                    ...
                    ...
                    'adds_on'       => array(
                        array(
                            'type'    => 'plain',
                            'content' => 'url-added-plain-part'
                        ),
                        array(
                            'type'    => 'replace',
                            'content' => 'url-added-replace-part/{{id}}'
                        ),
                    ),
                    ...
                    ...
                ),
                ...
                ...
            ),
        ),
        ...
        ...
    ),
);
 php


return array(
    ...
    ...
    'actions' => array(
        'action-group-1' => array(
            'action-name-1' => array(
                ...
                ...
                'request'           => array(
                    ...
                    ...
                    'headers'       => array( // Headers to be added to the requests with their default value
                        'Accept' => 'application/json',
                    ),
                    ...
                    ...
                ),
                ...
                ...
            ),
        ),
        ...
        ...
    ),
);
 php
    array(
        'Accept' => 'text/xml',
    )
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'request'           => array(
                    'type' => 'UrlEncoded',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'request'           => array(
                    'type' => 'UrlEncodedFromContent',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php
'Content-Type'=>'text/xml' 
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'request'           => array(
                    'type' => 'Xml',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'request'           => array(
                    'type' => 'PlainText',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php
'Content-Type'=>'application/json' 
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'request'           => array(
                    'type' => 'Json',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php


namespace Msl\Example\Request;

class GoogleRequestActionRequest extends AbstractActionRequest
{
    /**
     * Configures an action request with the given request values and content
     *
     * @param array  $requestValues      the request parameters
     * @param string $content            the body content
     * @param array  $urlBuildParameters the url build adds on parameter array
     * @param array  $headersValue       the header value array to override default header values
     *
     * @return mixed|void
     *
     * @throws \Msl\RemoteHost\Exception\BadConfiguredActionException
     */
    public function configure(array $requestValues, $content = "", array $urlBuildParameters = array(), array $headersValue = array())
    {
        // Set request parameters in parent entity
        parent::configure($requestValues, $content, $urlBuildParameters, $headersValue);

        // Set the request body content
        $this->setContent($content);
    }

    /**
     * Sets a proper EncType on the given \Zend\Http\Client object (for Xml Request, used value is Client::ENC_URLENCODED)
     *
     * @param \Zend\Http\Client $client the Zend http client object
     *
     * @return mixed|\Zend\Http\Client
     */
    public function setClientEncType(\Zend\Http\Client $client)
    {
        // Setting EncType to UrlEncoded
        $client->setEncType(\Zend\Http\Client::ENC_URLENCODED);

        return $client;
    }
}
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'request'           => array(
                    'type' => 'Msl\Example\Request\GoogleRequestActionRequest',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'response'           => array(
                    'type' => 'Json',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'response'           => array(
                    'type' => 'Xml',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'response'           => array(
                    'type' => 'PlainText',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php


namespace Msl\Example\Request;

class GoogleResponseActionResponse extends AbstractActionResponse
{
    /**
     * Converts the Response object body to an array
     *
     * @return array
     */
    public function bodyToArray()
    {
        // Getting response content
        $responseContent = $this->response->getContent();

        // We parse the content in a custom way and we convert it to an array...
        $customContentAsArray = array();

        ...........
        ...........
        ...........

        return $customContentAsArray;
    }
}
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'response'           => array(
                    'type' => 'Msl\Example\Response\GoogleResponseActionResponse',
                    ...
                ),
                ...
            ),
        ),
    ),
);
 php


use Msl\RemoteHost\Response\Wrapper\AbstractResponseWrapper;

/**
 * Json Google Response Wrapper for Google Actions
 */
class JsonGoogleResponseWrapper extends AbstractResponseWrapper
{
    /**
     * Defaults status strings
     */
    const STATUS_OK = "OK";

    /**
     * Initializes the object fields with the given raw data.
     *
     * @param array                    $rawData        array containing the response raw data
     * @param ActionResponseInterface  $actionResponse the action response object from which to extract additional information
     *
     * @return mixed
     */
    public function init(array $rawData, ActionResponseInterface $actionResponse)
    {
        // Setting raw data field
        $this->rawData = $rawData;

        // Setting status
        if (is_array($rawData) && isset($rawData['status'])) {
            if ($rawData['status'] === self::STATUS_OK) {
                $this->status = true;
            } else {
                $this->status = false;
            }
        } else {
            $this->status = false;
        }

        // Setting return code and return message
        $response = $actionResponse->getResponse();
        $this->returnCode    = $response->getStatusCode();
        $this->returnMessage = $response->getReasonPhrase();
    }

    /**
     * Returns the found routes
     *
     * @return array
     */
    public function getRoutes()
    {
        return $this->getBody();
    }

    /**
     * Return the body of the Response.
     *
     * @return array
     */
    public function getBody()
    {
        if (isset($this->rawData['routes'])) {
            return $this->rawData['routes'];
        }
        return array();
    }
}
 php


return array(
    'parameters' => array(
        ...
    ),
    ...
    'actions' => array(
        'example-api' => array(
            'action-1' => array(
                'name'              => 'scripts',
                'response'           => array(
                    'type'    => 'Json',
                    'wrapper' => 'Connector\Api\Google\ResponseWrapper\JsonGoogleResponseWrapper',
                ),
                ...
            ),
        ),
    ),
);
 php


return array(
    'parameters' => array(
        'host'      => 'http://www.example.com/api/', 
    ),
    'actions_parameters' => array(
    ),
    'config' => array(
        'maxredirects'  => 2,
        'timeout'       => 30,
        'adapter'       => 'Zend\Http\Client\Adapter\Curl',
    ),
    'actions' => array(
        'example-api' => array( 
            'script-list' => array( 
                'name'              => 'scripts', 
                'request'           => array(
                    'type'                     => 'UrlEncoded', 
                    'parameters'               => array( 
                        'format'      => 'json', 
                    ),
                ),
                'response' => array(
                    'type'      => 'Json', 
                ),
            ),
        ),
    ),
);
 php


return array(
    ...
    ...
    'actions' => array(
        'example-api' => array( 
            'script-list' => array( 
                ...
                ...
            ),
        ),
    ),
    ...
    ...    
);
 php
      
    ...
    ...
    // ApiImplementation is a child class of Msl\RemoteHost\Api\AbstractHostApi
    $api = new ApiImplementation();
    
    // Calling action script-list
    $result = $api->execute('example-api.script-list');
    ...
    ...    
 php
        
    ...
    ...
    // ApiImplementation is a child class of Msl\RemoteHost\Api\AbstractHostApi
    $api = new ApiImplementation();
        
    // Creating parameters array: here we can override all the default parameters defined in the configuration array
    $parameters = array('format'=>'xml');
    
    // Calling action script-list
    $result = $api->execute('example-api.script-list', $parameters);
 php

    ...
    ...
    // ApiImplementation is a child class of Msl\RemoteHost\Api\AbstractHostApi
    $api = new ApiImplementation();
        
    // Creating parameters array: here we can override all the default parameters defined in the configuration array
    $parameters = array();
    
    // Creating request content: this could be an XML content for example
    $content = 'text content';
    
    // Calling action script-list
    $result = $api->execute('example-api.script-list', $parameters, $content);
 php

    ...
    ...
    // ApiImplementation is a child class of Msl\RemoteHost\Api\AbstractHostApi
    $api = new ApiImplementation();

    // Creating parameters array: here we can override all the default parameters defined in the configuration array
    $parameters = array();

    // Creating request content: this could be an XML content for example
    $content = 'text content';

    // Calling action script-list
    $result = $api->execute('example-api.script-list', $parameters, $content);
 php

    ...
    ...
    // ApiImplementation is a child class of Msl\RemoteHost\Api\AbstractHostApi
    $api = new ApiImplementation();

    // Creating parameters array: here we can override all the default parameters defined in the configuration array
    $parameters = array();

    // Creating request content: this could be an XML content for example
    $content = 'text content';

    // Calling action script-list
    $result = $api->execute('example-api.script-list', $parameters, $content);
 php


return array(
    'parameters' => array(
        'host'      => 'http://maps.googleapis.com/maps/api/', // The host api
    ),
    'actions_parameters' => array(
    ),
    // For all possible values, please look at Zend\Http\Client->$config
    'config' => array(
        'maxredirects'  => 2,
        'timeout'       => 30,
        'adapter'       => 'Zend\Http\Client\Adapter\Curl',
    ),
    'actions' => array(
        'google-json' => array(
            'driving-directions' => array(
                'name'              => 'directions/json',
                'request'           => array(
                    'adds_on'       => array(
                        array ( // (e.g. http://maps.googleapis.com/maps/api/directions/json)
                            'type'    => 'plain',
                            'content' => 'directions/json'
                        )
                    ),
                    'type'                     => 'UrlEncoded',
                    'method'                   => 'GET', 
                    'parameters'               => array( 
                        'origin'      => '', // default value for each parameter; default values will be overriden with the values passed in the execute method
                        'destination' => '',
                        'sensor'      => '',
                    ),
                ),
                'response' => array(
                    'type'      => 'Json', 
                ),
            ),
        ),
    ),
);
 php


class GoogleApi extends AbstractHostApi
{
    /**
     * String containing the name of this api. This value will be used mainly for log purposes.
     *
     * @var string
     */
    const API_NAME = 'GOOGLE_API';

    /**
     * Returns the default config array
     *
     * @return mixed
     */
    public function getDefaultConfig()
    {
        return 
 php 


namespace Connector\Api\Google;

use Msl\RemoteHost\Api\AbstractHostApi;

class GoogleApi extends AbstractHostApi
{
    /**
     * String containing the name of this api. This value will be used mainly for log purposes.
     *
     * @var string
     */
    const API_NAME = 'GOOGLE_API';

    /**
     * Returns the default config array
     *
     * @return mixed
     */
    public function getDefaultConfig()
    {
        return sl\RemoteHost\Response\AbstractResponseWrapper
     */
    public function getRoutes($origin, $destination, $sensor)
    {

        /** @var /ResponseWrapper/JsonGoogleResponseWrapper $response */
        $response = null;
        try {
            $response = $this->execute(
                'google-json.driving-directions',
                array(
                    'origin'      => $origin,
                    'destination' => $destination,
                    'sensor'      => $sensor,
                )
            );
        } catch (\Exception $e) {
            echo sprintf('[%s] Google Host call failed! Error message is: \'%s\'', $this->getApiName(), $e->getMessage());
        }

        return $response;
    }
}