76 lines
2.1 KiB
PHP
76 lines
2.1 KiB
PHP
<?php
|
|||
|
|
// ============================================
|
||
|
|
// 文件: fetch_movie.php (OMDb 版)
|
||
|
|
// 说明: 根据电影名从 OMDb API 获取信息
|
||
|
|
// 用法: ?q=电影名称
|
||
|
|
// ============================================
|
||
|
|
|
||
|
|
// 填入你申请的 OMDb API Key
|
||
|
|
define('OMDB_API_KEY', 'f5a3828');
|
||
|
|
|
||
|
|
header('Content-Type: application/json; charset=utf-8');
|
||
|
|
|
||
|
|
$q = isset($_GET['q']) ? trim($_GET['q']) : '';
|
||
|
|
if (empty($q)) {
|
||
|
|
echo json_encode(['error' => '请输入电影名称']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
// OMDb API 请求 URL(按标题搜索,返回完整信息)
|
||
|
|
$url = 'http://www.omdbapi.com/?i=tt3896198&apikey=' . OMDB_API_KEY . '&t=' . urlencode($q) . '&plot=full';
|
||
|
|
|
||
|
|
// 发起请求
|
||
|
|
$ch = curl_init();
|
||
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'MovieLog/1.0');
|
||
|
|
$response = curl_exec($ch);
|
||
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
|
|
$curlError = curl_error($ch);
|
||
|
|
curl_close($ch);
|
||
|
|
|
||
|
|
if ($curlError) {
|
||
|
|
echo json_encode(['error' => '网络请求失败: ' . $curlError]);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($httpCode != 200) {
|
||
|
|
echo json_encode(['error' => 'API 返回 HTTP ' . $httpCode]);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
$data = json_decode($response, true);
|
||
|
|
if (!$data) {
|
||
|
|
echo json_encode(['error' => '解析数据失败']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查是否找到电影
|
||
|
|
if (isset($data['Response']) && $data['Response'] === 'False') {
|
||
|
|
echo json_encode(['error' => '未找到相关电影,请尝试更准确的关键词']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 提取需要的字段
|
||
|
|
$result = [
|
||
|
|
'title' => $data['Title'] ?? '',
|
||
|
|
'year' => $data['Year'] ?? '',
|
||
|
|
'rated' => $data['Rated'] ?? '',
|
||
|
|
'released'=> $data['Released'] ?? '',
|
||
|
|
'runtime' => $data['Runtime'] ?? '',
|
||
|
|
'genre' => $data['Genre'] ?? '',
|
||
|
|
'director'=> $data['Director'] ?? '',
|
||
|
|
'actors' => $data['Actors'] ?? '',
|
||
|
|
'plot' => $data['Plot'] ?? '',
|
||
|
|
'poster' => $data['Poster'] ?? '',
|
||
|
|
'imdbRating' => $data['imdbRating'] ?? '',
|
||
|
|
];
|
||
|
|
|
||
|
|
// 如果海报是 'N/A',置空
|
||
|
|
if ($result['poster'] === 'N/A') {
|
||
|
|
$result['poster'] = '';
|
||
|
|
}
|
||
|
|
|
||
|
|
echo json_encode($result);
|