DEV Community

Malte Riechmann for visuellverstehen

Posted on • Edited on • Originally published at happy-coding.visuellverstehen.de

This article has 61 positive reactions and 24 comments

TL;DR

Using the DEV API the title of this article gets automatically updated every 60 seconds.

Addicted to numbers

I started blogging on DEV only some months ago. You could say, I am quite new to all of this. After writing an article I find myself frequently checking the numbers of reactions and comments. It seems like I am a bit of an addict. And I bet some of you are too.

We share an addiction. We're approval junkies.

Jake Green, Revolver

It is not why I started this and I am sure it is not healthy. So I will try to stop and instead make this a bit of fun. Let's play around with the numbers.

APIs are the future

Back in 2010, I saw a tweet from Smashing Magazine asking about the future of the web. And I answered »APIs«, which is the same answer I would give today — 11 years later.

Let's have fun

It is so much fun working with well-implemented APIs and I was happy to find the DEV API as one of those.

My idea was simple:

  1. Get the properties of this article.
  2. Update the title using two of the properties (positive_reactions_count and comments_count).

The source

I use PHP, which is one of my favorite programming languages.

Get article properties

function getArticleProperties($articleId)
{
    // Prepare URL
    $url = 'https://dev.to/api/articles/' . $articleId;

    // Prepare headers
    $headers = [
        'api-key: 1234567890abcdef',
    ];

    // Prepare method
    $method = 'GET';

    // Execute request
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_URL, $url);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($curlHandle);
    curl_close($curlHandle);

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

Update article title

function updateArticleTitle($articleId, $articleTitle)
{
    // Prepare URL
    $url = 'https://dev.to/api/articles/' . $articleId;

    // Prepare payload
    $payload = json_encode(
        [
            'article' => [
                'title' => $articleTitle,
            ],
        ]
    );

    // Prepare headers
    $headers = [
        'Content-Type: application/json',
        'Content-Length: ' . strlen($payload),
        'api-key: 1234567890abcdef',
    ];

    // Prepare method
    $method = 'PUT';

    // Execute request
    $curlHandle = curl_init();
    curl_setopt($curlHandle, CURLOPT_URL, $url);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
    $response  = curl_exec($curlHandle);
    curl_close($curlHandle);
}
Enter fullscreen mode Exit fullscreen mode

Putting it all together

// Prepare article ID
$articleId = 715066;

// Get article properties using the API
$articleProperties = getArticleProperties($articleId);

// Update article title using the API
updateArticleTitle($articleId, 'This article has ' . $articleProperties['positive_reactions_count'] . ' positive reactions and ' . $articleProperties['comments_count'] . ' comments');
Enter fullscreen mode Exit fullscreen mode

A cronjob is executing this as a CLI script every 60 seconds.

Inspiration

This article is heavily inspired by an awesome YouTube video I saw earlier this year.

Top comments (24)

Collapse
 
natalia_asteria profile image
Natalia Asteria

Tom Scott...

Collapse
 
codereviewpad profile image
Reviewpad

Haha, totally.

Collapse
 
malteriechmann profile image
Malte Riechmann

What about him?

Collapse
 
janschill profile image
Jan Schill
Thread Thread
 
janschill profile image
Jan Schill

You even have it in your Inspiration section, haha oops

Thread Thread
 
malteriechmann profile image
Malte Riechmann

Yes, I am totally aware of Tom Scott and his awesome video. That is why I put it into my article.

Thanks for making it clear ✌️

Collapse
 
hasnaindev profile image
Muhammad Hasnain

Haha, yeah. But his one's are actually stuck, right?

Collapse
 
natalia_asteria profile image
Natalia Asteria

Not sure.

Collapse
 
hasnaindev profile image
Muhammad Hasnain

Could you please tell me how you run a cronjob in PHP? JavaScript is easy, you run an app and it is like a software. PHP runs only when a user or some external event hits an endpoint to trigger all the code? Please, correct me if I'm wrong. Thanks!

Collapse
 
nilamo profile image
Alex Winfield

phpcli is bundled and enabled by default with all php installations, allowing php files to be executed via the command line just like any other interpreted language, and thus can be added to a crontab.

Collapse
 
hasnaindev profile image
Muhammad Hasnain

Thank you Alex!

Collapse
 
sarmd23 profile image
Sarmad

Oh good

Collapse
 
malteriechmann profile image
Malte Riechmann

👍

Collapse
 
sebastiandg7 profile image
Sebastián Duque G

I'm here just to make my comment count!

Collapse
 
malteriechmann profile image
Malte Riechmann

Well, it counts.

Collapse
 
felixdorn profile image
Félix Dorn

Gotta test if it still works :)

Collapse
 
malteriechmann profile image
Malte Riechmann

Still looks good. Thanks for testing.

Collapse
 
shrn profile image
Sharan J

<3

Collapse
 
malteriechmann profile image
Malte Riechmann

<3

Collapse
 
christiankozalla profile image
Christian Kozalla

Nice, very impressive! I like to test this with comments =)

Collapse
 
malteriechmann profile image
Malte Riechmann

Thanks for testing. It seems to work.

Collapse
 
sm0ke profile image
Sm0ke

Hacky & creative ... Nice

Collapse
 
malteriechmann profile image
Malte Riechmann

Thank you, Sm0ke.

Collapse
 
yamen profile image
Yamen Hadla

comment number 24 :D