This article has 61 positive reactions and 24 comments
Tags: showdev, writing, meta, webdev
Published: 2021-06-04 07:43:51

## 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](https://www.youtube.com/watch?v=GkEE3gRX0kQ)

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](https://twitter.com/smashingmag/status/9646126079) from Smashing Magazine asking about the future of the web. And I answered »[APIs](https://twitter.com/malteriechmann/status/9646229572)«, 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](https://docs.forem.com/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

```php
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);
}
```

### Update article title

```php
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);
}
```

### Putting it all together

```php
// 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');
```

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

## Inspiration

This article is heavily inspired by [an awesome YouTube video](https://www.youtube.com/watch?v=BxV14h0kFs0) I saw earlier this year.