DEV Community

Cover image for How to use the dev.to API!
Saymon Tavares
Saymon Tavares

Posted on • Edited on

How to use the dev.to API!

Why?

I use dev.to as my main dev blogging area now, but I want to show-off my blogs on my portfolio! So we're going to create a simple list of the latest 3 blog articles which link back to the dev.to site. For this tutorial I'll be showing how this can be achieved in PHP.

Let's go!

We're going to be using this endpoint: https://dev.to/api/articles?username=saymontavares which generates a JSON object with the author's latest 30 articles. To get your personal endpoint, change the nataliedeweerd username to your own.

So how do we get this data into our website? In PHP we can use something called cURL. cURL (client URL) is a PHP library which allows you to make HTTP requests. So you can call a URL in your code and get a HTML response from it.

The below code shows a basic curl function which gets us our data:

$curl = curl_init();
$username = "saymontavares";
$url = "https://dev.to/api/articles?username={$username}";

curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => [
        "cache-control: no-cache"
    ]
]);

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

curl_close($curl);
Enter fullscreen mode Exit fullscreen mode

OR

$username = "saymontavares";
$url = "https://dev.to/api/articles?username={$username}";
$res = file_get_contents($url);
$res = json_decode($res, true);
Enter fullscreen mode Exit fullscreen mode

@chrisshennan thanks!

We need to decode this data before we can effectively use it however.

$res = json_decode($res, true);
Enter fullscreen mode Exit fullscreen mode

This decodes the json object into a much more usable array! All we need to do now is loop through the array, and print out the results.

foreach ($res as $article) {
    echo    "<a href='{$article['url']}' class='article'>
                <h2>{$article['title']}</h2>
                <div class='description'>{$article['description']}</div>
                <div class='readmore'>Read More</div>
            </a>";
}
Enter fullscreen mode Exit fullscreen mode

And that's it! We just need to apply a bit of CSS and our dev.to articles are printing out wherever we want!


I hope you enjoyed the read!

Feel free to follow me on GitHub, LinkedIn and DEV for more!

Top comments (3)

Collapse
 
chrisshennan profile image
Chris Shennan

With the cURL version you reference

$res = curl_exec($curl);
Enter fullscreen mode Exit fullscreen mode

but to decode it you reference

$response = json_decode($response, true);
Enter fullscreen mode Exit fullscreen mode

$response is not defined in your code but $res is so I believe it should be

$res = json_decode($res, true);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
chrisshennan profile image
Chris Shennan

I really like these type of posts, small, clean and straight to the point.

Have you considered using file_get_contents instead of cURL? file_get_contents works with URLs and since this is a straight forward GET request (no authentication or API keys required) it would work fine, make the code a little simpler and reduce the function to get the data to 4 lines

$username = "saymontavares";
$url = "https://dev.to/api/articles?username={$username}";
$res = file_get_contents($url);
$res = json_decode($res, true);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
artydev profile image
artydev

Hello,
Nice, you can look here DevAPI

And test here DEVAPI