hooshid/rottentomatoes-scraper

从rottentomatoes.com获取电影、电视信息的库

2.4.2 2024-07-31 06:54 UTC

This package is auto-updated.

Last update: 2024-09-13 06:32:41 UTC


README

Build Status Total Downloads Latest Stable Version License

使用此Rottentomatoes API,您能够搜索、浏览并提取rottentomatoes.com上电影、电视剧的数据。

安装

此库抓取rottentomatoes.com,因此网站的变化可能会使此库的部分功能失效。您可能每年需要更新几次。

要求

  • PHP >= 7.3
  • PHP cURL 扩展

通过composer安装

$ composer require hooshid/rottentomatoes-scraper

运行示例

示例为您提供了一个快速演示,以确保一切正常,一些示例代码,并让您轻松查看一些可用的数据。

从此存储库根目录中的示例文件夹启动php的内置web服务器,并浏览到 https://:8000

php -S localhost:8000

示例

获取电影/电视剧数据

电影:The Matrix (1999) / 网址:https://www.rottentomatoes.com/m/matrix

$rottentomatoes = new Hooshid\RottentomatoesScraper\Rottentomatoes();
$extract = $rottentomatoes->extract("/m/matrix");
$result = $extract['result'];
$error = $extract['error'];

// get all available data as json
echo json_encode($extract);

在上面的示例中,我们首先从Rottentomatoes()类创建一个新的obj,然后调用extract方法,并将rottentomatoes.com的url作为第一个参数传入。

如果一切正常,result键被填充,如果不正常,错误键被填充了发生的错误

电视剧:Game of Thrones (2011-2019) / 网址:https://www.rottentomatoes.com/tv/game_of_thrones

$rottentomatoes = new Hooshid\RottentomatoesScraper\Rottentomatoes();
$extract = $rottentomatoes->extract("/tv/game_of_thrones");
$result = $extract['result'];
$error = $extract['error'];

if ($error) {
    echo $error;
} else {
    echo $result['title']; // movie/series title
    echo $result['thumbnail']; // Poster thumbnail
    echo $result['summary']; // Summary
    
    echo $result['score']; // Score
    echo number_format($result['votes']); // Votes
    echo $result['user_score']; // User Score
    echo number_format($result['user_votes']); // User Votes
}

您必须始终首先捕获错误并获取结果。

注意:您可以传递Rottentomatoes的完整网址或页面路径

extract("https://www.rottentomatoes.com/m/matrix");
extract("/m/matrix");

两种提取方法的结果相同!

搜索

$rottentomatoes = new Hooshid\RottentomatoesScraper\Rottentomatoes();
$result = $rottentomatoes->search("The Matrix", "movie");

if($result['result']) {
    foreach ($result['result'] as $row) {
        echo $row['thumbnail'];
        echo $row['title'];
        echo $row['full_url'];
        echo $row['title']; 
        echo $row['year'];
        echo $row['score']; 
        echo $row['user_score']; 
        echo $row['type'];
    }
} else {
    echo "Not found any result!";
}

搜索方法总是返回result键,您只需循环并使用即可。搜索方法有两个参数,第一个是电影或电视剧的标题,第二个是类型,类型只能是movie或tv。

名人

$rottentomatoes = new Hooshid\RottentomatoesScraper\Rottentomatoes();
$result = $rottentomatoes->celebrity("johnny_depp");

if($result['result']) {
    echo $result['result']['name'];
    echo $result['result']['full_url'];
    echo $result['result']['url_slug'];
    echo $result['result']['thumbnail'];
    echo $result['result']['bio'];
    // Movies : array
    foreach ($result['result']['movies'] as $row) {
        echo $row['title'];
        echo $row['url'];
        echo $row['year'];
        echo $row['tomatometer'];
        echo $row['audiencescore'];
    }
    // Series : array
    foreach ($result['result']['series'] as $row) {
        echo $row['title'];
        echo $row['url'];
        echo $row['year'];
        echo $row['tomatometer'];
        echo $row['audiencescore'];
    }
} else {
    echo "Not found!";
}

完整示例

只需打开示例文件夹,我们在那里放了一些示例和方法的演示供您使用!

相关项目

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件