← Back
Editing: instamatic-api.php
<?php class InstamaticInstagramGraph { private string $accessToken; private ?string $proxy; public function __construct(string $accessToken, ?string $proxy = null) { $this->accessToken = $accessToken; $this->proxy = $proxy; } private function request(string $endpoint, string $method = 'GET', array $params = []) : array { $base = 'https://graph.facebook.com/'; $url = $base . ltrim($endpoint, '/'); $params['access_token'] = $this->accessToken; $args = [ 'method' => strtoupper($method), 'timeout' => 45, 'redirection' => 5, 'sslverify' => false, ]; if ($args['method'] === 'GET') { $url = add_query_arg($params, $url); } else { $args['body'] = $params; } if (!empty($this->proxy)) { $parts = wp_parse_url($this->proxy); if (is_array($parts) && !empty($parts['host'])) { $host = $parts['host']; $port = isset($parts['port']) ? (int)$parts['port'] : 8080; $args['proxy'] = $host . ':' . $port; if (!empty($parts['user']) || !empty($parts['pass'])) { $args['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode(($parts['user'] ?? '') . ':' . ($parts['pass'] ?? '')); } } } $res = wp_remote_request($url, $args); if (is_wp_error($res)) { throw new Exception('Instagram Graph request failed: ' . $res->get_error_message()); } $code = (int) wp_remote_retrieve_response_code($res); $body = (string) wp_remote_retrieve_body($res); $json = json_decode($body, true); if (!is_array($json)) { throw new Exception('Instagram Graph invalid JSON response (HTTP ' . $code . '): ' . $body); } if (isset($json['error'])) { $msg = $json['error']['message'] ?? 'Unknown Graph error'; $usr = $json['error']['error_user_msg'] ?? ''; throw new Exception(trim($msg . ' ' . $usr)); } return $json; } private function checkUploadStatus(string $creationId) : bool { @set_time_limit(0); for ($i = 0; $i < 30; $i++) { $status = $this->request($creationId, 'GET', ['fields' => 'status_code']); if (!isset($status['status_code'])) { return false; } if (in_array($status['status_code'], ['EXPIRED', 'ERROR'], true)) { return false; } if ($status['status_code'] === 'IN_PROGRESS') { sleep(3); continue; } return true; } return false; } public function uploadPhotoToFeed(string $igUserId, string $imageUrl, string $caption = '') : array { $upload = $this->request($igUserId . '/media', 'POST', [ 'image_url' => $imageUrl, 'caption' => $caption, ]); if (empty($upload['id'])) { throw new Exception('Instagram upload did not return a creation id.'); } if (!$this->checkUploadStatus((string)$upload['id'])) { throw new Exception('Instagram did not accept the media you wanted to upload. It may violate publishing requirements. [Upload ID: ' . $upload['id'] . ']'); } $publish = $this->request($igUserId . '/media_publish', 'POST', [ 'creation_id' => $upload['id'], ]); if (empty($publish['id'])) { throw new Exception('Instagram publish did not return a media id.'); } $shortcode = $this->request($publish['id'], 'GET', ['fields' => 'shortcode']); return [ 'id' => (string)($shortcode['shortcode'] ?? ''), 'id2' => (string)$publish['id'], ]; } public function uploadReel(string $igUserId, string $videoUrl, string $caption = '') : array { $upload = $this->request($igUserId . '/media', 'POST', [ 'media_type' => 'REELS', 'video_url' => $videoUrl, 'caption' => $caption, ]); if (empty($upload['id'])) { throw new Exception('Instagram reel upload did not return a creation id.'); } if (!$this->checkUploadStatus((string)$upload['id'])) { throw new Exception('Instagram did not accept the reel you wanted to upload. [Upload ID: ' . $upload['id'] . ']'); } $publish = $this->request($igUserId . '/media_publish', 'POST', [ 'creation_id' => $upload['id'], ]); if (empty($publish['id'])) { throw new Exception('Instagram reel publish did not return a media id.'); } $shortcode = $this->request($publish['id'], 'GET', ['fields' => 'shortcode']); return [ 'id' => (string)($shortcode['shortcode'] ?? ''), 'id2' => (string)$publish['id'], ]; } public function uploadStoryImage(string $igUserId, string $imageUrl) : array { $upload = $this->request($igUserId . '/media', 'POST', [ 'media_type' => 'STORIES', 'image_url' => $imageUrl, ]); if (empty($upload['id'])) { throw new Exception('Instagram story upload did not return a creation id.'); } if (!$this->checkUploadStatus((string)$upload['id'])) { throw new Exception('Instagram did not accept the story media. [Upload ID: ' . $upload['id'] . ']'); } $publish = $this->request($igUserId . '/media_publish', 'POST', [ 'creation_id' => $upload['id'], ]); if (empty($publish['id'])) { throw new Exception('Instagram story publish did not return a media id.'); } return [ 'id' => '', 'id2' => (string)$publish['id'], ]; } }
Save File
Cancel