Are there Concrete helper functions which do the same as:
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode($data),
'header' => "Content-Type: application/json\r\n" . "Accept: application/json\r\n",
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
mesuva
December 21, 2021, 8:21pm
#2
Yes, the HTTP Client helper:
https://documentation.concretecms.org/developers/appendix/http-client
Your code would look like:
$client = app()->make('http/client/curl');
$client->setMethod('POST');
$client->setEncType('application/json');
$client->setRawBody(json_encode($data);
$client->setUri($url);
$response = $client->send();
Pretty sure that hasn’t change in V9 from V8.
1 Like
Thank you for the starting point. I think the Zend is gone from v9, looks like there’s a GuzzleHttpClient now. Hope it’s more or less similar to what I need. I don’t need curl.
I got it. This:
$client = $app->make(GuzzleHttpClient::class);
$result = $client->post($url, [
GuzzleHttpClientRequestOptions::JSON => $data
]);
$response = json_decode($result->getBody()->getContents());
is equivalent to this:
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode($data),
'header' => "Content-Type: application/json\r\n" . "Accept: application/json\r\n",
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
Could anyone tell me what would be equivalent to:
$request = file_get_contents('php://input');
with GuzzleHttp Client? I can’t find any examples of it reading the php input.
mesuva
December 22, 2021, 11:27am
#5
I didn’t realise that - that’s a little annoying, as it means V8 code with documented helpers isn’t backwards compatible. The documentation needs updating then.
I do appreciate the decision to change to Guzzle though - it’s very well established and documented, and used in many other libraries and systems.
Yep, your example matches what I’ve just been adjusting, which looks pretty much like this now:
$baseURL = "https://someurl";
$data = ['foo'=>'bar'];
$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();
$version = $app->make('config')->get('concrete.version');
if (version_compare($version, '9.0', '<')) {
$client = $app->make('http/client/curl');
$client->setMethod('POST');
$client->setEncType('application/json');
$client->setRawBody(json_encode($data);
$client->setUri($baseURL);
$response = $client->send();
$returnBody = $response->getBody();
} else {
$client = new \GuzzleHttp\Client();
$headers = ['json'=> $data];
$response = $client->request('POST', $baseURL);
$returnBody = $response->getBody()->getContents();
}
$return = json_decode($returnBody, true);
There might be a better way to test for v9, but I think this is at least clear as an example.
I don’t think you would access php://input
with Guzzle as its for remote calls. php://input
is really just a low-level way of accessing request data.