How to properly use curl_init for a GET request in PHP? or Why is my curl_init GET request not workin

24 Replies, 2015 Views

"Hey guys, struggling with a curl_init get request in PHP. Anyone got a simple example?"

So I'm trying to fetch some data from an API using curl_init get request, but it's not returning what I expect.

Maybe I'm missing something basic? Like, do I need to set CURLOPT_RETURNTRANSFER or something?

Here's what I’ve got:

```php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/api");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
```

Is this correct? Or am I totally off?

Also, any best practices for handling errors? Sometimes it just fails silently and I’m like... *why?*

Thanks in advance! 🙏

Messages In This Thread
How to properly use curl_init for a GET request in PHP? or Why is my curl_init GET request not workin - by - 19-06-2024, 03:42 PM



Users browsing this thread: 1 Guest(s)