This content originally appeared on DEV Community and was authored by Malte Riechmann
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 out the numbers. It seems like I am a bit addicted to those. I think some of you are alike.
We share an addiction. We're approval junkies.
It is not why I was starting 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 way I would answer today — 11 years later.
Let's have fun
It makes 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:
- Get the properties of this article.
- Update the title using two of the properties (
positive_reactions_count
andcomments_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);
}
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);
}
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');
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.
This content originally appeared on DEV Community and was authored by Malte Riechmann

Malte Riechmann | Sciencx (2021-06-04T07:43:51+00:00) This article has 5 positive reactions and 2 comments. Retrieved from https://www.scien.cx/2021/06/04/this-article-has-5-positive-reactions-and-2-comments/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.