Skip to content

Commit

Permalink
fix: accessing array offset on null (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yizack authored Jul 22, 2024
1 parent 443cf2c commit 54a41e9
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/InstagramFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,41 @@
require_once "Config.php";

class InstagramFeed extends Config {
protected const TOKEN_REFRESH_INTERVAL = 1; // 24 hours in seconds

protected function request($path) {
protected function fetch($path) {
return json_decode(file_get_contents($path), true);
}

protected function refreshToken() {
$path = $this->getPath();
$filename = $this->getFilename();
$filePath = "$path/$filename";
$date = date("Y-m-d H:i:s");
$array = ["updated" => $date];

if (!file_exists($path)) {
mkdir($path, 0777, true);
$fp = fopen("$path/$filename", "w");
fwrite($fp, json_encode($array));
fclose($fp);
}

$date_json = $this->request("$path/$filename")["updated"];

if (strtotime($date) - strtotime($date_json) > 86400) {
$this->request("https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=" . $this->getToken());
$array = ["updated" => $date];
$fp = fopen("$path/$filename", "w");
fwrite($fp, json_encode($array));
fclose($fp);

if (!file_exists($filePath)) {
file_put_contents($filePath, json_encode($array));
}

$updatedJson = $this->fetch($filePath);
$updatedDate = $updatedJson["updated"] ?? $date;

if (strtotime($date) - strtotime($updatedDate) > self::TOKEN_REFRESH_INTERVAL) {
$this->fetch("https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=" . $this->getToken());
file_put_contents($filePath, json_encode($array));
}
}

function getFeed($fields = null) {
$fields = $fields ?? 'username,permalink,timestamp,caption';
$this->refreshToken();
return $this->request("https://graph.instagram.com/me/media?fields=" . $fields . "&access_token=" . $this->getToken())["data"];
$feed = $this->fetch("https://graph.instagram.com/me/media?fields=" . $fields . "&access_token=" . $this->getToken());
return $feed["data"] ?? [];
}
}
?>

0 comments on commit 54a41e9

Please sign in to comment.