How to implement feed
Let's take a test url, for example (the number in the url is the user ID):
http://rusconashine.com/hu/user-feed/225
If we call this url, for example in a browser, we get a json response with the user's posts.
The structure will look like this:
[ { "post":{ "created":{ "date": string, "time": string }, "text": string, "url": string, "image": string, "likeCount": int, "commentCount":int, "hasMethod": bool }, "user":{ "avatar": string, "url": string, "nick": string } } ]
Contains an array of objects, where each object is one post detail.
The object has two parts, content information and user information.
I'll describe on the list
- post - information about the post
-
created - date of post created
- date - first part of date (for example 02.05.2022)
- time - second part of date (for example 11:25:56)
- text - post descriptions
- url - post url
- image - post image url
- likeCount - number of likes on this post
- commentCount - number of comments on this post
- hasMethod - if it contains a solution procedure
-
created - date of post created
-
user - information about the user
- avatar - avatar url
- url - user detail url
- nick - user nickname
Now there is nothing stopping us from implementing our feed anywhere on the web.
Processing in PHP is very simple. For example, we can read the resulting json using file_get_contents and then convert it to an object or array using the json_decode function.
We can do this easily, for example as follows:
// load url from server with json $json = file_get_contents($url); // parse json to array $obj = json_decode($json, true);
Now we have the entire result array in the obj variable and we can just write it out, using iteration.
The result may then look like this:
Nude nails🤎🤍 #nails #instanails #nailovers #nailstyle #beauty #nailsinspiration #rusconalover #czech_nails #gelovamodelaz #nudenails #nailsdesign
post detailEuphoria💞 #nailart #pinknails #gelnails #nailsdesing #rusconacz #naillove #nailsporn #rusconagels
post detailCat eye 02🤩❤️ #nailart #cateyenails #cateye02 #rusconacolors #rusconalover #gelnails #gelnailsdesign #nailslove #czechnails
post detailThe whole code in PHP can then be written like this:
function processFeed(string $url): array { // load url from server with json $json = file_get_contents($url); // parse json to array $obj = json_decode($json, true); // create html template $tpl = '<div class="sample"> <a href="{user.url}" target="_blank"><strong<{user.nick}<strong/<</a> <span>{post.created.date} {post.created.time}</span> <img src="{post.image}"> <p>{post.text}</p> <a target="_blank" href="{post.url}">post detail</a> </div>'; $finalArray = []; // iterate array with posts foreach ($obj as $posts) { // process array to make variables $data = processFeedArray($posts); $item = $tpl; // replace in template foreach ($data as $key => $value) { $item = str_replace('{' . $key . '}', $value, $item); } $finalArray[] = $item; // only first 3 items if (count($finalArray) >= 3) { break; } } return $finalArray; } /** * @param array $array * @param string $keyPrefix * @return array */ public processFeedArray(array $array, string $keyPrefix = ''): array { $out = []; // iterate array with data and prepare keys foreach ($array as $key => $value) { if (is_array($value)) { // call again $out = $out + $this->processFeedArray($value, ($keyPrefix ? $keyPrefix . '.' : '') . $key); } else { // create key $out[$keyPrefix . '.' . $key] = $value; } } return $out; } echo implode(processFeed('url'));