PHP: cURL Api Endpoint To Retrieve Data

The below code is used to request data from an API, there are additional settings within that allow you to send the likes of headers to the API, this is typically needed to validate your API key.

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://jacksonhogg-bitool-staging.herokuapp.com/job-board-posts');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('api-key: MaXxVN6PVheQEMTRVTreApdBDfDjNZYQ7NWh4DAqzKGMScXYww'));
    
    $output = curl_exec($ch);
    $decode_output = json_decode($output, TRUE);

    foreach($decode_output as $jobPost) {
        echo '<div style="background: grey; color: white; padding: 20px;margin-bottom: 20px;">';
        echo '<p>'.$jobPost['id'].'</p>';
        echo '<p>'.$jobPost['countryName'].'</p>';
        echo '<p>'.$jobPost['dateAdded'].'</p>';
        echo '<p>'.$jobPost['description'].'</p>';
        echo '<p>'.$jobPost['employmentType'].'</p>';
        echo '<p>'.$jobPost['payRate'].'</p>';
        echo '<p>'.$jobPost['publicDescription'].'</p>';
        echo '<p>'.$jobPost['responseUserBullhornId'].'</p>';
        echo '<p>'.$jobPost['salary'].'</p>';
        echo '<p>'.$jobPost['salaryUnit'].'</p>';
        echo '<p>'.$jobPost['startDate'].'</p>';
        echo '<p>'.$jobPost['title'].'</p>';
        echo '<p>'.$jobPost['willRelocate'].'</p>';
        echo '<p>'.$jobPost['willSponsor'].'</p>';
        echo '<p>'.$jobPost['yearsRequired'].'</p>';
        echo '</div>';
    }

The code explained…

In order to initialise the cURL request, we need to use a function provided by PHP called curl_init(). We assign this to a variable as you have to pass the curl_init() value into multiple other functions. This value is also known as the curl handler.

We then call the following block of code:

curl_setopt($ch, CURLOPT_URL, 'https://jacksonhogg-bitool-staging.herokuapp.com/job-board-posts');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('api-key: MaXxVN6PVheQEMTRVTreApdBDfDjNZYQ7NWh4DAqzKGMScXYww'));

Breaking this down line by line, I’ll first explain curl_setopt. This function again is part of the cURL library PHP uses, and it allows us to set multiple options for the cURL request before it’s sent. If you don’t set any options nothing will have as we’ve not instructed it what the URL to cURL is, this brings me on to this line in particular:

curl_setopt($ch, CURLOPT_URL, 'https://jacksonhogg-bitool-staging.herokuapp.com/job-board-posts');

In this code the curl_setopt() function takes 3 parameters, the 1st is the cURL handler which I explained above. The 2nd is the CURLOPT_XXX this is the name of the option you wish to define in the 3rd parameter, here is a link to all the options available when calling CURLOPT_XXX. Click here. The 3rd parameter as mentioned briefly, is the value you’re assigning to the option, you’ll need to read the link prior to see what value each option accepts as they differ from boolean, integer, array, string etc.

In our example we make use of the following options:

CURLOPT_URL //this option is for setting the URL we wish to cURL, this accepts a string value

CURLOPT_RETURNTRANSFER //this option is for stating if we want to receive the data back, it accepts a boolean of true/false

CURLOPT_HTTPHEADER //this option is for setting headers to be sent with your cURL request, this accepts an array of strings (most commonly used to set an api key as we have in our example).

The next piece of code we use is the curl_exec() function, this takes the cURL handler as the argument and then executes that cURL request. We choose to store the output of the cURL in a variable so we can use it else where with ease.

The next piece of code we use is the json_decode($output, TRUE) function, the response we get back from our cURL is usually in a JSON format, this means to manipulate the data in PHP we need to decode the json. This takes 4 parameters but we only make use of 2, the 1st is the json to decode and the 2nd is for setting an associative array or not this will in most cases want to be set to true.

And finally we use a simple PHP forloop to loop over the data. In order to know what the output data is so you can echo it else where use the following:

foreach($decode_output as $jobPost) {
    var_dump($jobPost);
}

Powered by BetterDocs