How to properly use CURL_INIT for a GET request? or Why is my CURL_INIT GET request not working as ex

16 Replies, 1015 Views

CURL_INIT GET requests can be tricky at first. Here’s a breakdown:

1. Initialize with `curl_init()` and the URL.
2. Set `CURLOPT_RETURNTRANSFER` to true so the response is saved, not echoed.
3. Execute with `curl_exec()`.

Example:
```php
$ch = curl_init("https://reqres.in/api/users?page=2");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($data));
```

Without RETURNTRANSFER, you’d just see the output directly—no control over it.

Messages In This Thread



Users browsing this thread: 1 Guest(s)