diff --git a/docs/API_WORKFLOW_WORDPRESS_N8N.md b/docs/API_WORKFLOW_WORDPRESS_N8N.md index d196dc1..fd4a74f 100644 --- a/docs/API_WORKFLOW_WORDPRESS_N8N.md +++ b/docs/API_WORKFLOW_WORDPRESS_N8N.md @@ -196,6 +196,196 @@ curl -s -X POST "https://api.momentry.ddns.net/api/v1/search" \ --- +## WordPress PHP 範例 + +### 基本設定 + +```php + $method, + 'headers' => [ + 'X-API-Key' => self::API_KEY, + 'Content-Type' => 'application/json', + ], + 'timeout' => 30, + ]; + + if ($data !== null) { + $args['body'] = json_encode($data); + } + + $response = wp_remote_request($url, $args); + + if (is_wp_error($response)) { + throw new Exception($response->get_error_message()); + } + + return json_decode(wp_remote_retrieve_body($response), true); + } + + public static function getVideos(): array { + return self::request('GET', '/api/v1/videos'); + } + + public static function getVideo(string $uuid): array { + return self::request('GET', "/api/v1/videos/{$uuid}"); + } + + public static function getJob(string $uuid): array { + return self::request('GET', "/api/v1/jobs/{$uuid}"); + } + + public static function search(string $query, int $topK = 5): array { + return self::request('POST', '/api/v1/search', [ + 'query' => $query, + 'top_k' => $topK, + ]); + } +} +``` + +### Step 3: 確認註冊成功 + +```php + '', + 'limit' => 10, + ], $atts); + + if (empty($atts['query'])) { + return '

請輸入搜尋關鍵字

'; + } + + try { + $results = Momentry_API::search($atts['query'], $atts['limit']); + + if (empty($results['results'])) { + return '

找不到相關結果

'; + } + + $html = '
'; + $html .= '

搜尋結果: ' . esc_html($atts['query']) . '

'; + $html .= '
'; + return $html; + + } catch (Exception $e) { + return '

搜尋服務暫時無法使用

'; + } +}); +``` + +**使用方式**: +```html +[momentry_search query="關鍵字" limit="5"] +``` + +--- + +## 完整 n8n Workflow 範例 + +``` +┌──────────────┐ +│ 觸發 (定時) │ +└──────┬───────┘ + ▼ +┌──────────────┐ ┌──────────────┐ +│ 查詢影片 │────►│ 比對新檔案 │ +│ /videos │ │ │ +└──────┬───────┘ └──────────────┘ + │ │ + ▼ ▼ +┌──────────────┐ ┌──────────────┐ +│ 等待處理 │◄────│ 輪詢任務狀態 │ +│ /jobs/:uuid │ │ /jobs/:uuid │ +└──────┬───────┘ └──────────────┘ + │ + ▼ (completed) +┌──────────────┐ +│ 搜尋測試 │ +│ /search │ +└──────────────┘ +``` + +--- + **注意**: - 處理時間視影片長度而定(1分鐘影片約需 2-5 分鐘處理) - 大量影片時建議分批上傳