【问题标题】:how to connect ombd api using php script如何使用 php 脚本连接 ombd api
【发布时间】:2019-07-20 09:21:12
【问题描述】:

我已经为网站创建了一个搜索选项,以使用 mysql 数据库获取电影详细信息,但我想使用 omdb api 连接它们,但我不知道请帮我解决这个问题

这是我使用mysql数据库的数据库连接

<?php

//variables
$server = "localhost";
$username = "root";
$password = "";
$dbname = "devsearch";


$conn = mysqli_connect($server, $username, $password, $dbname);

OMDb API:http://www.omdbapi.com/?i=tt3896198&apikey=[MY_API_KEY]

【问题讨论】:

  • 您的意思是要将 API 中的电影存储到您的数据库中,还是只搜索它?
  • 只需搜索并显示例如:标题年份等
  • 我不太明白你的问题。您能告诉我们您的 MySQL 数据库中存储了哪些信息吗?那么,当您使用已有的搜索选项进行搜索时,会返回什么? IMDb ID?你想用它从开放电影数据库中检索信息吗?他们的 API 返回 JSON 或 XML。您是否有关于如何使用 PHP 脚本检索此 JSON 或 XML 的问题?
  • 我想从打开的电影数据库中检索信息?

标签: php mysql omdbapi


【解决方案1】:

如果您的问题是关于如何使用 PHP 脚本从开放电影数据库中检索信息,您可以使用以下 PHP 代码:

function getImdbRecord($ImdbId, $ApiKey)
{
    $path = "http://www.omdbapi.com/?i=$ImdbId&apikey=$ApiKey";
    $json = file_get_contents($path);
    return json_decode($json, TRUE);
}

$data = getImdbRecord("tt3896198", "f3d054e8");

echo "<pre>";
print_r($data);
echo "</pre>";

如果您在读取 ​​URL 时遇到问题,请阅读 the tip in the PHP manual about fopen wrappers

上面的代码会返回这个结果:

Array
(
    [Title] => Guardians of the Galaxy Vol. 2
    [Year] => 2017
    [Rated] => PG-13
    [Released] => 05 May 2017
    [Runtime] => 136 min
    [Genre] => Action, Adventure, Comedy, Sci-Fi
    [Director] => James Gunn
    [Writer] => James Gunn, Dan Abnett (based on the Marvel comics by), Andy Lanning (based on the Marvel comics by), Steve Englehart (Star-Lord created by), Steve Gan (Star-Lord created by), Jim Starlin (Gamora and Drax created by), Stan Lee (Groot created by), Larry Lieber (Groot created by), Jack Kirby (Groot created by), Bill Mantlo (Rocket Raccoon created by), Keith Giffen (Rocket Raccoon created by), Steve Gerber (Howard the Duck created by), Val Mayerik (Howard the Duck created by)
    [Actors] => Chris Pratt, Zoe Saldana, Dave Bautista, Vin Diesel
    [Plot] => The Guardians struggle to keep together as a team while dealing with their personal family issues, notably Star-Lord's encounter with his father the ambitious celestial being Ego.
    [Language] => English
    [Country] => USA
    [Awards] => Nominated for 1 Oscar. Another 12 wins & 42 nominations.
    [Poster] => https://m.media-amazon.com/images/M/MV5BMTg2MzI1MTg3OF5BMl5BanBnXkFtZTgwNTU3NDA2MTI@._V1_SX300.jpg
    [Ratings] => Array
        (
            [0] => Array
                (
                    [Source] => Internet Movie Database
                    [Value] => 7.7/10
                )

            [1] => Array
                (
                    [Source] => Rotten Tomatoes
                    [Value] => 84%
                )

            [2] => Array
                (
                    [Source] => Metacritic
                    [Value] => 67/100
                )

        )

    [Metascore] => 67
    [imdbRating] => 7.7
    [imdbVotes] => 489,848
    [imdbID] => tt3896198
    [Type] => movie
    [DVD] => 22 Aug 2017
    [BoxOffice] => $389,804,217
    [Production] => Walt Disney Pictures
    [Website] => https://marvel.com/guardians
    [Response] => True
)

【讨论】:

  • 谢谢你,请告诉我如何分隔标题并单独显示?
  • @NAND 你可以用echo $data["Title"];显示标题
【解决方案2】:

您正在访问 Json API。您可以使用

提取 JSON 数据

json_decode()

此链接对您有帮助How do I extract data from JSON with PHP?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多