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.
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:
- 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.
Top comments (24)
Tom Scott...
Haha, totally.
What about him?
youtu.be/BxV14h0kFs0
You even have it in your Inspiration section, haha oops
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 ✌️
Haha, yeah. But his one's are actually stuck, right?
Not sure.
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!
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.
Thank you Alex!
Oh good
👍
I'm here just to make my comment count!
Well, it counts.
Gotta test if it still works :)
Still looks good. Thanks for testing.
<3
<3
Nice, very impressive! I like to test this with comments =)
Thanks for testing. It seems to work.
Hacky & creative ... Nice
Thank you, Sm0ke.
comment number 24 :D