DEPRECATION WARNING
This documentation is not using the current rendering mechanism and is probably outdated. The extension maintainer should switch to the new system. Details on how to use the rendering mechanism can be found here.
Developer’s manual¶
Suppose, for instance, an Extbase controller action:
$httpClientRequest = $this->objectManager->get('TS\Restclient\Client\HttpClientRequest')->setMethod('GET')->setUrl('http://domain/api/users/123/profile');
$httpClient = $this->objectManager->get('TS\Restclient\Client\HttpClient')->setErrorThrowException(true)->setRequest($httpClientRequest);
try {
$httpClient->exec();
$httpClientResponse = $httpClient->getResponse();
$userProfile = $httpClientResponse->getBody();
}
catch (HttpClientException $e) {
$errorCode = $e->getCode();
$errorMEssage = $e->getMessage();
}
You can use HttpClient method doRequest setting the request in-line:
$httpClient = $this->objectManager->get('TS\Restclient\Client\HttpClient')->setErrorThrowException(true);
try {
$httpClient->doRequest('GET', 'http://domain/api/users/123/profile');
$httpClientResponse = $httpClient->getResponse();
$userProfile = $httpClientResponse->getBody();
}
catch (HttpClientException $e) {
$errorCode = $e->getCode();
$errorMEssage = $e->getMessage();
}
Also you can use the CRUD shorthands:
try {
$userProfile = $this->objectManager->get('TS\Restclient\Client\HttpClient')->setErrorThrowException(true)->get('http://domain/api/users/123/profile');
}
catch (HttpClientException $e) {
$errorCode = $e->getCode();
$errorMEssage = $e->getMessage();
}
If you need to to send json with the request:
$data = array('message' => 'It\'s just a message sent with RESTclient.');
$dataJson = json_encode($data);
$httpClientRequest = $this->objectManager->get('TS\Restclient\Client\HttpClientRequest')
-> setMethod('post')
-> setUrl('http://domain/api/users/123/feed')
-> setHeader(array('Content-Type: application/json', 'Content-Length: '.strlen($dataJson)))
-> setData($dataJson)
$httpClient = $this->objectManager->get('TS\Restclient\Client\HttpClient')->setErrorThrowException(true)->setRequest($httpClientRequest);
try {
$httpClient->exec();
$httpClientResponse = $httpClient->getResponse();
$userProfile = $httpClientResponse->getBody();
}
catch (HttpClientException $e) {
$errorCode = $e->getCode();
$errorMEssage = $e->getMessage();
}
If you need to set the HTTP header and to send files/data fields with the request:
$httpClientRequest = $this->objectManager->get('TS\Restclient\Client\HttpClientRequest')
-> setMethod('post')
-> setUrl('http://domain/api/users/123/feed')
-> setHeader(array('Authorization: OAuth 1234567890'))
-> setData(array('message' => 'It\'s just a message sent with RESTclient.'))
-> setFiles array('file1'=> array('filename'=> 'file1.txt', 'realpath'=> '/path/to/file1', 'mimetype'=>'txt'));
$httpClient = $this->objectManager->get('TS\Restclient\Client\HttpClient')->setErrorThrowException(true)->setRequest($httpClientRequest);
try {
$httpClient->exec();
$httpClientResponse = $httpClient->getResponse();
$userProfile = $httpClientResponse->getBody();
}
catch (HttpClientException $e) {
$errorCode = $e->getCode();
$errorMEssage = $e->getMessage();
}
If you don’t set errorThrowException, you can analyze the HttpClientError:
$httpClientRequest = $this -> objectManager -> get('TS\Restclient\Client\HttpClientRequest')-> setMethod('get')->setUrl('http://domain/api/users/123/profile')
$httpClient = $this->objectManager->get('TS\Restclient\Client\HttpClient')->setRequest($httpClientRequest);
$httpClientResponse = $httpClient->exec()->getResponse();
if ($httpClientResponse === false) {
$errorCode = $httpClient->getError()->getCode();
$errorMessage = $httpClient->getError()->getMessage();
}
API Reference¶
This table provides the description of HttpClient* classes.
Class Summary | Description |
---|---|
\TS\Restclient\Client\HttpClient | The class that you must use to do the request and to receive the response. |
\TS\Restclient\Client\HttpClientRequest | The request. |
\TS\Restclient\Client\HttpClientResponse | The response of HttpClient request. |
\TS\Restclient\Client\HttpClientError | If you don’t set throwErrorException, in case of failure you still can get an instance of HttpClientError from HttpClient. |
\TS\Restclient\Client\HttpClientException | The Exception class thrown from HttpClient in case of failure. |
This table provides how to use the methods provided by HttpClient.
Method summary |
---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This table provides how to use the methods provided by HttpClientRequest.
Method summary |
---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This table provides how to use the methods provided by HttpClientResponse.
Method summary |
---|
|
|
|
|
|
|
|
|
This table provides how to use the methods provided by HttpClientError.
Method summary |
---|
|
|
This table provides how to use the methods provided by HttpClientException.
Method summary |
---|
|
|
This table provides the description of the error codes.
Code | Description |
---|---|
-2 | An error occurs when creating an instance of the client. |
-4 | An error occurs when setting an option. |
-8 | It has been requested unsupported method. |
-16 | An error occurs when execute the request. |
Hook¶
Before the HttpClient returns the response to you, you can use this hook to process it:
Add in your ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/restclient/Classes/HttpClient.php']['responsePreProcess'][] = '\Namespace\Foo';
In your extkey/Classes/Foo.php:
namespace \Namespace;
class Foo {
public function responsePreProcess(&$response, $pObj) {
//process $response
}
}