Wallogit.com
2017 © Pedro Peláez
JSON-RCP simple client.
This repository provide simple curl usage, (*1)
composer require rgen3/restapi-core-client
Code example usage is in example folder, (*2)
<?php
use rgen3\json\client\core\AbstractMethod;
class TestRequest extends AbstractMethod
{
public function getBaseUrl()
{
return 'https://httpstat.us';
}
public function getUrlPath()
{
return '/200';
}
public function getType()
{
return 'GET';
}
public function getData()
{
// Sends GET data to sleep 50 seconds
return [
'sleep' => '2000'
];
}
public function waitAnswer()
{
// if you do not want to wait for an answer
// set here false
return true;
}
}
After creating file call it in your code, (*3)
// data you want to send
$data = [
'any' => 'array'
];
$request = (new TestRequest($data))->executer();
$request->getResult();
// and same method
$request->result;
// Get curl response headers
$request->getCurl()->getResponseHeaders();
// Get response status
$request->getResponseStatus();
if you want to provide token as a GET parameter than you have to use, (*4)
rgen3\json\client\core\Fabric::setToken('needed-token');
before any method requiring the token called, (*5)
Note you cannot use methods related with curl response when you are using async mode
(when waitAnswer returns false), (*6)
You can use any method below in your TestRequest class to modify behaviour, (*7)
```php, (*8)
/**
* Makes request asynchronous
*
* You woun't waste time waiting for a request answer
* But you will not get any response
* for an answer,
*
* @return bool
*/
public function waitAnswer()
{
// if you do not want to wait for an answer
// set return value to false
return true;
}, (*9)
/** * Sets the type of the request * i.e. 'GET', 'POST', 'PUT', 'DELETE', etc. * * @return mixed */ public function getType() { // Default value to be returned return 'POST'; }, (*10)
public function getBaseUrl() { // base url you want to use for this method return 'https://httpstat.us'; }, (*11)
/** * Sets the url of a method to be call * * @return mixed */ public function getUrlPath() { // path without url // slashes will be trimmed return '/pathname'; }, (*12)
/**
* Returns the data to be send
*
* @return mixed
*/
public function getData()
{
// return any data you want
// as default you have to json compatible data
// because of json_encode method in AbstractMethod class
return [];
}, (*13)
/** * Processes * * @param $data * @return IMethod */ public function processResult($data);, (*14)
/** * Returns request results * @return mixed */ public function getResult() { // returns result from curl return []; }, (*15)
/** * @return array */ public function getCurlOptions() { // Here you can provide additional curl parameters return []; }, (*16)
/** * @return bool */ public function tokenRequired() { // return true if you want to send token as get parameter return true; }, (*17)
/** * Returns token key * @return string */ public function getTokenKey() { // You can set any value you want to see in GET query parameter for token // default is return 'access-token'; }, (*18)
/** * Prepares data to be sent * For all method except GET * @return array */ public function getRequestData() { $array = []; // any code you want return $array; } ```, (*19)