# WordPress 呼叫 Momentry API 指南 | 項目 | 內容 | |------|------| | 版本 | V1.0 | | 日期 | 2026-03-23 | | 用途 | 在 WordPress 中呼叫 Momentry API | --- ## API URL 在 WordPress 中呼叫 API,**請使用外部 URL**: ``` https://api.momentry.ddns.net ``` > ⚠️ WordPress 運行於瀏覽器端,無法直接訪問 `localhost`。 --- ## 常用端點 | 方法 | 端點 | 說明 | |------|------|------| | GET | `/health` | 健康檢查 | | POST | `/api/v1/search` | 語意搜尋(標準格式) | | GET | `/api/v1/videos` | 列出所有影片 | | GET | `/api/v1/lookup` | 查詢影片 | --- ## PHP 範例 ### 基本搜尋 ```php 'charade', 'limit' => 10 ]; $response = wp_remote_post($api_url, [ 'headers' => ['Content-Type' => 'application/json'], 'body' => json_encode($data), 'timeout' => 30 ]); if (is_wp_error($response)) { echo '錯誤: ' . $response->get_error_message(); } else { $body = json_decode(wp_remote_retrieve_body($response), true); print_r($body['hits']); } ?> ``` ### 列出所有影片 ```php 30]); if (!is_wp_error($response)) { $body = json_decode(wp_remote_retrieve_body($response), true); foreach ($body['videos'] as $video) { echo $video['file_name'] . "\n"; } } ?> ``` ### 查詢特定影片 ```php 30]); if (!is_wp_error($response)) { $video = json_decode(wp_remote_retrieve_body($response), true); echo '檔案: ' . $video['file_name'] . "\n"; echo '時長: ' . $video['duration'] . ' 秒'; } ?> ``` --- ## JavaScript 範例 ### 使用 fetch ```javascript // 搜尋影片 async function searchVideos(query, limit = 10) { const response = await fetch('https://api.momentry.ddns.net/api/v1/n8n/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query, limit }) }); if (!response.ok) { throw new Error('API 請求失敗'); } return await response.json(); } // 使用範例 searchVideos('charade', 5) .then(data => { data.hits.forEach(hit => { console.log(`${hit.text} (score: ${hit.score})`); }); }); ``` --- ## WordPress Shortcode 範例 在 `functions.php` 中註冊短碼: ```php '', 'limit' => '10' ], $atts); if (empty($atts['query'])) { return '
請提供搜尋關鍵字
'; } $response = wp_remote_post('https://api.momentry.ddns.net/api/v1/n8n/search', [ 'headers' => ['Content-Type' => 'application/json'], 'body' => json_encode([ 'query' => $atts['query'], 'limit' => (int)$atts['limit'] ]), 'timeout' => 30 ]); if (is_wp_error($response)) { return '搜尋服務暫時無法使用
'; } $data = json_decode(wp_remote_retrieve_body($response), true); if (empty($data['hits'])) { return '找不到相關結果
'; } $output = '