K
K
Katya Smirnova2020-04-13 15:43:45
PHP
Katya Smirnova, 2020-04-13 15:43:45

PHP cURL get request?

Good day, you need to make a request to the API via a GET request, in the body of which json is passed. I understand that the answer is most likely the simplest, but I could not implement this in PHP via cURL.

Let's say I would need to make a POST request, then everything is really simple

$url = 'http://www.example.com/api';

$ch = curl_init($url);
$data = array(
    'username' => 'codexworld',
    'password' => '123456'
);
$payload = json_encode(array("user" => $data));

curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

curl_close($ch);


But how do you do the same with a GET request?

By
Mo
Gi
Te

## There is no way to use third-party libraries, ONLY CURL.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sundukov, 2020-04-13
@sireax

A GET request must not have a body.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question