Back to all articles
Simple cURL api connection to GET data from PHP rest API

Simple cURL api connection to GET data from PHP rest API

Updated on December 3, 2023

Looking for a new website?

Get in touch
Save your eyes, enable dark-mode!

Need to connect data from an API to your website using WordPress or any other PHP-based CMS? With a cURL (client URL) method, it’s easy to GET and POST data. We already covered a more in-depth solution in our previous cURL API POST/GET/PUT/DELETE with PHP article, but I would also like to give you a fundamental GET example to get data from an API endpoint simply.

The basic cURL setup

We cannot avoid a minimal setup before making a cURL connection. It needs at least a few parameters to be able to work. Mainly you also need to get the API key to make sure you’re able to connect if your data is not publicly available.

$apiKey = 'd8965...2f'; // your own API key
$endpoint = '';

$curl = curl_init();
curl_setopt_array($curl, [
  CURLOPT_URL => $endpoint,
      "x-api-key: $apiKey"

$response = curl_exec($curl);
$err = curl_error($curl);


Most of these are default settings you can adjust to fit your needs. The only required ones are obviously the $endpoint (the URL you want to call) and the $apiKey to be able to connect with your account.

With that piece of code, we have the results of our cURL connection stored in a $resposne variable. To be able to read this data, we need to make sure we didn’t receive an error, and if not, show our result.

We can do that by adding the following snippet:

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  $res = json_decode($response, true);
  $jobsArray = $res['data'];

In my example, I transform the JSON response to a PHP array. This made it easier for me to loop over the response and showcase what I needed.

But this is a simpler way of showing you a working cURL API GET example with PHP. If you need more functionalities or flexibility in your cURL API calls, please read my Comprehensive Guide to Making REST API Calls with CURL’s GET Method.

Leave a Reply

Your email address will not be published. Required fields are marked *