PHP Examples

Introduction

This guide provides vanilla PHP examples for integrating Feedframer into your PHP applications without frameworks.

Basic Usage

Simple GET Request

<?php

$apiKey = 'YOUR_API_KEY';
$url = "https://feedframer.com/api/v1/me?api_key={$apiKey}&page[size]=12";

$response = file_get_contents($url);
$data = json_decode($response, true);

foreach ($data['posts'] as $post) {
    echo '<img src="' . htmlspecialchars($post['mediaUrl']) . '" alt="" />';
}

Using cURL

<?php

function fetchPosts($apiKey, $params = []) {
    $defaultParams = [
        'api_key' => $apiKey,
        'page[size]' => 12,
    ];

    $queryParams = array_merge($defaultParams, $params);
    $url = 'https://feedframer.com/api/v1/me?' . http_build_query($queryParams);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if ($httpCode !== 200) {
        return null;
    }

    return json_decode($response, true);
}

$apiKey = 'YOUR_API_KEY';
$data = fetchPosts($apiKey, ['filter[type]' => 'IMAGE']);

if ($data && isset($data['posts'])) {
    foreach ($data['posts'] as $post) {
        echo '<div class="post">';
        echo '<img src="' . htmlspecialchars($post['mediaUrl']) . '" />';
        echo '<p>' . htmlspecialchars($post['caption']) . '</p>';
        echo '</div>';
    }
}

Complete Example Class

<?php

class FeedframerClient {
    private $apiKey;
    private $baseUrl = 'https://feedframer.com';

    public function __construct($apiKey) {
        $this->apiKey = $apiKey;
    }

    public function getPosts($params = []) {
        $defaultParams = [
            'api_key' => $this->apiKey,
            'page[size]' => 12,
        ];

        $queryParams = array_merge($defaultParams, $params);
        $url = "{$this->baseUrl}/api/v1/me?" . http_build_query($queryParams);

        return $this->makeRequest($url);
    }

    public function getImagePosts($limit = 12) {
        return $this->getPosts([
            'filter[type]' => 'IMAGE',
            'page[size]' => $limit,
        ]);
    }

    public function getVideoPosts($limit = 12) {
        return $this->getPosts([
            'filter[type]' => 'VIDEO',
            'page[size]' => $limit,
        ]);
    }

    private function makeRequest($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Accept: application/json',
        ]);

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error = curl_error($ch);

        curl_close($ch);

        if ($error) {
            error_log("Feedframer API Error: {$error}");
            return null;
        }

        if ($httpCode !== 200) {
            error_log("Feedframer API returned status {$httpCode}");
            return null;
        }

        $data = json_decode($response, true);

        if (isset($data['errors'])) {
            error_log("Feedframer API Error: " . print_r($data['errors'], true));
            return null;
        }

        return $data;
    }
}

// Usage
$feedframer = new FeedframerClient('YOUR_API_KEY');
$data = $feedframer->getImagePosts(12);

if ($data && isset($data['posts'])) {
    foreach ($data['posts'] as $post) {
        // Render post
    }
}

Caching with File System

<?php

class CachedFeedframerClient extends FeedframerClient {
    private $cacheDir;
    private $cacheDuration = 3600; // 1 hour

    public function __construct($apiKey, $cacheDir = '/tmp/feedframer-cache') {
        parent::__construct($apiKey);
        $this->cacheDir = $cacheDir;

        if (!is_dir($this->cacheDir)) {
            mkdir($this->cacheDir, 0755, true);
        }
    }

    public function getPosts($params = []) {
        $cacheKey = md5(serialize($params));
        $cacheFile = "{$this->cacheDir}/{$cacheKey}.json";

        // Check cache
        if (file_exists($cacheFile)) {
            $cacheAge = time() - filemtime($cacheFile);

            if ($cacheAge < $this->cacheDuration) {
                $cached = file_get_contents($cacheFile);
                return json_decode($cached, true);
            }
        }

        // Fetch fresh data
        $data = parent::getPosts($params);

        if ($data) {
            file_put_contents($cacheFile, json_encode($data));
        }

        return $data;
    }

    public function clearCache() {
        $files = glob("{$this->cacheDir}/*.json");
        foreach ($files as $file) {
            unlink($file);
        }
    }
}

HTML Template

Complete example with HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Instagram Feed</title>
    <style>
        .instagram-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 1rem;
            max-width: 1200px;
            margin: 0 auto;
            padding: 2rem;
        }

        .instagram-post {
            position: relative;
            overflow: hidden;
            border-radius: 8px;
        }

        .instagram-post img {
            width: 100%;
            height: 300px;
            object-fit: cover;
            display: block;
        }

        .instagram-caption {
            padding: 0.5rem;
            font-size: 14px;
            color: #333;
        }
    </style>
</head>
<body>
    <h1>Latest from Instagram</h1>

    <div class="instagram-grid">
        <?php
        require_once 'FeedframerClient.php';

        $feedframer = new CachedFeedframerClient('YOUR_API_KEY');
        $data = $feedframer->getImagePosts(9);

        if ($data && isset($data['posts'])) {
            foreach ($data['posts'] as $post) {
                ?>
                <div class="instagram-post">
                    <a href="<?php echo htmlspecialchars($post['permalink']); ?>"
                       target="_blank">
                        <img src="<?php echo htmlspecialchars($post['mediaUrl']); ?>"
                             alt="<?php echo htmlspecialchars($post['caption'] ?? ''); ?>" />
                    </a>

                    <?php if (!empty($post['caption'])): ?>
                        <div class="instagram-caption">
                            <?php echo htmlspecialchars(mb_substr($post['caption'], 0, 100)); ?>
                        </div>
                    <?php endif; ?>
                </div>
                <?php
            }
        } else {
            echo '<p>No posts found.</p>';
        }
        ?>
    </div>
</body>
</html>

Next Steps