Showing posts with label fetch. Show all posts
Showing posts with label fetch. Show all posts

Fetch posts with labels - render in html - caching

<div class="jobs">


<?php


/* =========================

   CONFIG

========================= */


$label = 'Bank%20Jobs';

$keyword = 'apprentice';


$feedUrl = 'https://govtjobsportal.in/feeds/posts/summary/?max-results=150&alt=json&category=' . $label;


$cacheDir  = __DIR__ . '/cache';

$cacheFile = $cacheDir . '/bank-jobs.json';


$cacheTime = 3600; // 1 hour


$lockFile = $cacheDir . '/cache.lock';




/* =========================

   INIT CACHE FOLDER

========================= */


if (!is_dir($cacheDir)) {

    mkdir($cacheDir, 0755, true);

}




/* =========================

   CHECK CACHE VALIDITY

========================= */


$cacheExists = file_exists($cacheFile);

$isExpired = !$cacheExists || (time() - filemtime($cacheFile) > $cacheTime);




/* =========================

   LOCK SYSTEM (IMPORTANT)

   PREVENTS CACHE STAMPEDE

========================= */


$lock = fopen($lockFile, 'c');


if ($isExpired) {


    if (flock($lock, LOCK_EX | LOCK_NB)) {


        // Only ONE request will refresh cache


        $context = stream_context_create([

            'http' => [

                'timeout' => 10

            ]

        ]);


        $json = @file_get_contents($feedUrl, false, $context);


        if ($json) {

            file_put_contents($cacheFile, $json);

        }


        flock($lock, LOCK_UN);

    }

}




/* =========================

   LOAD CACHE (FALLBACK SAFE)

========================= */


if (file_exists($cacheFile)) {

    $json = file_get_contents($cacheFile);

} else {

    echo "<p>Data temporarily unavailable.</p>";

    exit;

}


$data = json_decode($json, true);


if (empty($data['feed']['entry'])) {

    echo "<p>No jobs found.</p>";

    exit;

}




/* =========================

   DISPLAY RESULTS

========================= */


foreach ($data['feed']['entry'] as $entry) {


    $title = $entry['title']['$t'] ?? '';


    if (stripos($title, $keyword) === false) {

        continue;

    }


    $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');


    $summary = strip_tags($entry['summary']['$t'] ?? '');

    $summary = mb_substr($summary, 0, 150);

    $summary = htmlspecialchars($summary, ENT_QUOTES, 'UTF-8');


    $url = '';


    foreach ($entry['link'] as $link) {

        if (($link['rel'] ?? '') === 'alternate') {

            $url = $link['href'];

            break;

        }

    }


    $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');


    $date = date('j F Y', strtotime($entry['published']['$t'] ?? ''));


    ?>


    <div class="bs-jobs">


        <h2 class="bs-job-title"><?= $title ?></h2>


        <table class="bs-job-table">

            <tbody>

                <tr>

                    <td>Education Qualification</td>

                    <td>10th Pass</td>

                </tr>

                <tr>

                    <td>Location Eligibility</td>

                    <td>Sasaram</td>

                </tr>

                <tr>

                    <td>Post Date</td>

                    <td><?= $date ?></td>

                </tr>

                <tr>

                    <td>Employment Notification</td>

                    <td>

                        <a href="<?= $url ?>" target="_blank" rel="nofollow">

                            Job Details

                        </a>

                    </td>

                </tr>

            </tbody>

        </table>


    </div>


    <?php

}


fclose($lock);


?>


</div>

related.php - filters similar job names

$category $ post_1 merged

  • $category = 'Chemical Engineer'
  • $post_1 = Chemical Engineer, AO, JE, Clerk, MTS
Then
  • Final Result will be Chemical Engineer, AO, JE, Clerk, MTS
  • Why Needed?
  • On Chemical Engineering Jobs Page
  • I want to show the profile Chemical Engineer first then other jobs follows.

Fetch Recommended Listings in Tables

Fetch Recommended Listings in Tables

Fetch Recommended Blogs in Tables

Fetch Recommended Blogs in Tables

Fetch Similar Blogs

Fetch Similar Blogs

Fetch Blocks

Fetch Blocks

Fetch Similar Listings

Fetch Similar Listings

Fetch Recommended Blogs in Listings

Fetch Recommended Blogs

Fetch Listings - Previous and Next

Fetch Previous (Old) and Next (New) Listings

Fetch Listings by Tags

Fetch by tags

  • single
  • multiple
  • static (manual - nurse)
  • dynamic (auto-generated - $tag1)
Union VS Intersect
  1. withTagsUnion()
  2. withTagsIntersect()

Partials File that Fetches Recommended Listings - recommended-listings.php

<?php if (!$recommendedListings->isEmpty()): ?>
<section class="mb-5">
<h2 class="mb-4">Recommended for You</h2>
<div class="row">
<?php foreach ($recommendedListings as $recommendedListing): ?>

Partials File that Fetches Recommended Blogs - recommended-blogs.php

<?php if (!$recommendedBlogs->isEmpty()): ?>
<section class="mb-5">
<h2 class="mb-4">Recommended Blogs</h2>
<div class="row">
<?php foreach ($recommendedBlogs as $blog): ?>

Fetch tag-wise Listings in Listings

<!-- DEMONSTRATION: Recommended Listings (Similar by Tags) -->
<?php $recommendedListings = $listing->fetchRecommended(5, true); ?>

<?php if (!$recommendedListings->isEmpty()): ?>
<section class="mb-5">
<h2 class="mb-4">Recommended for You</h2>
<p class="text-muted mb-3">
<em>Using: <code>$listing->fetchRecommended(3)</code> - Listings sharing tags with this one</em>
</p>
<div class="row">
<?php foreach ($recommendedListings as $recommendedListing): ?>

Fetch tag-wise Listings

How to Fetch tag-wise Listings?

Fetch tag-wise Blogs

How to Fetch tag-wise Blogs?