Use Guzzle instead of cURL
21. Juli 2022
21. Juli 2022
In some projects you may need to use cURL to call remote URLs via GET or POST. The RequestFactory is a core class that simplifies this task.
The RequestFactory uses the PSR-7 standard for requests, responses and streams. Behind the scenes, Guzzle uses the best method available (cURL, sockets, etc), which is even more flexible than simple cURL.
RequestFactory | https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Http/Index.html |
Guzzle | https://docs.guzzlephp.org/en/latest/ |
<?php
$data = [
'param1' => 'Nice',
'param2' => 'here',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://yoururl/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
if ($info['http_code'] == 200) {
//oleole
}
<?php
$options = [
'form_params' => [
'param1' => 'Nice',
'param2' => 'here',
]
];
$requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
$response = $requestFactory->request('https://yoururl', 'POST', $options);
if ($response->getStatusCode() === 200) {
//oleole
}