S
S
symnoob2018-07-03 11:26:06
symfony
symnoob, 2018-07-03 11:26:06

Symfony 4 - how to make a POST request to someone else's (external) API?

having read the documentation and did not become smarter, can anyone tell me how to send a request to posts with a couple of parameters and get JSON back?

http://symfony.com/doc/2.6/book/http_fundamentals.html#requests-and-responses-in-symfony

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry, 2018-07-03
@php10

https://github.com/guzzle/guzzle

K
Kirill Gorelov, 2018-07-03
@Kirill-Gorelov

For example line 145
https://gist.github.com/Kirill-Gorelov/50e412984b1...

P
Pavel Novikov, 2018-07-03
@paulfcdd

Try using cURL like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'www.someapi.com?param1=A&param2=B');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); // Assuming you're requesting JSON
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);

// If using JSON...
$data = json_decode($response);

G
GTRxShock, 2018-07-03
@GTRxShock

if many such requests are planned, put the wrapper
https://github.com/8p/EightPointsGuzzleBundle
if the request is single, then you can get by with a curl, there are examples in the topic

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question