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

16 Replies, 1098 Views

Here’s a simple example of a CURL_INIT GET request in PHP. This hits a dummy API and returns the response.

```php
$ch = curl_init("https://jsonplaceholder.typicode.com/posts/1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

echo $response;
```

CURLOPT_RETURNTRANSFER is *kinda* necessary—without it, curl_exec() just dumps the output instead of storing it in a variable. If you wanna process the response, you need it.

For testing, Postman or Insomnia are great tools to mock requests before coding.

Messages In This Thread



Users browsing this thread: 1 Guest(s)