# 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 = ''; return $output; }); ?> ``` **使用方式**: ``` [momentry_search query="charade" limit="5"] ``` --- ## REST API Endpoint (WP >= 5.0) 在 WordPress REST API 中註冊自定義端點: ```php 'POST', 'callback' => function($request) { $response = wp_remote_post( 'https://api.momentry.ddns.net/api/v1/n8n/search', [ 'headers' => ['Content-Type' => 'application/json'], 'body' => json_encode([ 'query' => $request->get_param('query'), 'limit' => $request->get_param('limit', 10) ]) ] ); if (is_wp_error($response)) { return new WP_Error('api_error', 'API 請求失敗'); } return json_decode(wp_remote_retrieve_body($response)); } ]); }); ?> ``` **呼叫方式**: ``` POST /wp-json/momentry/v1/search Body: {"query": "charade", "limit": 5} ``` --- ## 常見錯誤 ### 錯誤: cURL error 7 **原因**: 無法連接到 API **檢查**: 1. API 服務是否啟動 2. 網路是否可達 ### 錯誤: 502 Bad Gateway **原因**: API 服務未啟動 **解決**: ```bash sudo launchctl load /Library/LaunchDaemons/com.momentry.api.plist ``` --- ## curl 測試 在終端機中測試: ```bash # 健康檢查 curl https://api.momentry.ddns.net/health # 搜尋測試 curl -X POST https://api.momentry.ddns.net/api/v1/n8n/search \ -H "Content-Type: application/json" \ -d '{"query":"charade","limit":5}' ``` --- ## 相關文件 - [API_INDEX.md](./API_INDEX.md) - 文件總覽 - [API_ENDPOINTS.md](./API_ENDPOINTS.md) - 端點完整說明 - [API_N8N_GUIDE.md](./API_N8N_GUIDE.md) - n8n 使用範例