maximeb97/trends-php
Composer 安装命令:
composer require maximeb97/trends-php
包简介
Google Trends API for PHP
关键字:
README 文档
README
A PHP library for interacting with the Google Trends API. This package provides a simple and type-safe way to access Google Trends data programmatically.
This is a PHP port of the TypeScript library @shaivpidadi/trends-js, maintaining identical functionality and behavior.
Installation
composer require maximeb97/trends-php
Requirements
- PHP >= 7.4
- cURL extension enabled
Features
- Get daily trending topics
- Get real-time trending topics
- Get autocomplete suggestions
- Explore trends data
- Get interest by region data
- Get related topics for any keyword
- Get related queries for any keyword
- Get combined related data (topics + queries)
- Full error handling with custom exceptions
Usage
Basic Usage
<?php require_once 'vendor/autoload.php'; use Maximeb97\GoogleTrends\GoogleTrends; // Get daily trends $result = GoogleTrends::dailyTrends(['geo' => 'US', 'lang' => 'en']); if (isset($result['data'])) { $trends = $result['data']; foreach ($trends->allTrendingStories as $story) { echo $story->title . " - Traffic: " . $story->traffic . "\n"; } } else { echo "Error: " . $result['error']->getMessage() . "\n"; }
Daily Trends
Get daily trending topics for a specific region:
use Maximeb97\GoogleTrends\GoogleTrends; $result = GoogleTrends::dailyTrends([ 'geo' => 'US', // Default: 'US' 'lang' => 'en', // Default: 'en' ]); // Result structure: // [ // 'data' => DailyTrendingTopics { // allTrendingStories: TrendingStory[], // summary: TrendingTopic[] // } // ] // or // ['error' => GoogleTrendsError]
Real-Time Trends
Get real-time trending topics:
use Maximeb97\GoogleTrends\GoogleTrends; use Maximeb97\GoogleTrends\Types\GoogleTrendsTrendingHours; $result = GoogleTrends::realTimeTrends([ 'geo' => 'US', // Default: 'US' 'trendingHours' => GoogleTrendsTrendingHours::FOUR_HOURS, // Default: 4 ]); // Available trending hours: // - GoogleTrendsTrendingHours::FOUR_HOURS (4) // - GoogleTrendsTrendingHours::ONE_DAY (24) // - GoogleTrendsTrendingHours::TWO_DAYS (48) // - GoogleTrendsTrendingHours::SEVEN_DAYS (168)
Autocomplete
Get search suggestions for a keyword:
$result = GoogleTrends::autocomplete( 'bitcoin', // Keyword to get suggestions for 'en-US' // Language (default: 'en-US') ); // Returns: ['data' => string[]] or ['error' => GoogleTrendsError] if (isset($result['data'])) { print_r($result['data']); // Array of suggestion strings }
Explore
Get widget data for a keyword:
$result = GoogleTrends::explore([ 'keyword' => 'bitcoin', 'geo' => 'US', // Default: 'US' 'time' => 'today 12-m', // Default: 'today 12-m' 'category' => 0, // Default: 0 'property' => '', // Default: '' 'hl' => 'en-US', // Default: 'en-US' ]); // Returns: ['widgets' => array] or ['error' => GoogleTrendsError]
Interest By Region
Get interest data by geographic region:
$result = GoogleTrends::interestByRegion([ 'keyword' => 'bitcoin', 'startTime' => new DateTime('2023-01-01'), // Default: new DateTime('2004-01-01') 'endTime' => new DateTime(), // Default: now 'geo' => 'US', // Default: 'US' 'resolution' => 'REGION', // Default: 'REGION' (can be 'COUNTRY', 'REGION', 'CITY', 'DMA') 'hl' => 'en-US', // Default: 'en-US' 'timezone' => 0, // Default: current timezone offset 'category' => 0, // Default: 0 ]); // Returns: array with geoMapData or ['error' => GoogleTrendsError]
Related Topics
Get related topics for a keyword:
$result = GoogleTrends::relatedTopics([ 'keyword' => 'bitcoin', 'geo' => 'US', // Default: 'US' 'time' => 'now 1-d', // Default: 'now 1-d' 'category' => 0, // Default: 0 'property' => '', // Default: '' 'hl' => 'en-US', // Default: 'en-US' ]); // Returns: ['data' => ['default' => ['rankedList' => [...]]]] or ['error' => GoogleTrendsError] if (isset($result['data'])) { $rankedList = $result['data']['default']['rankedList']; foreach ($rankedList as $list) { foreach ($list['rankedKeyword'] as $topic) { echo $topic['topic']['title'] . "\n"; } } }
Related Queries
Get related queries for a keyword:
$result = GoogleTrends::relatedQueries([ 'keyword' => 'bitcoin', 'geo' => 'US', // Default: 'US' 'time' => 'now 1-d', // Default: 'now 1-d' 'hl' => 'en-US', // Default: 'en-US' ]); // Returns: ['data' => ['default' => ['rankedList' => [...]]]] or ['error' => GoogleTrendsError] if (isset($result['data'])) { $rankedList = $result['data']['default']['rankedList']; foreach ($rankedList as $list) { foreach ($list['rankedKeyword'] as $query) { echo $query['query'] . " - " . $query['formattedValue'] . "\n"; } } }
Related Data (Combined)
Get combined related topics and queries:
$result = GoogleTrends::relatedData([ 'keyword' => 'bitcoin', 'geo' => 'US', // Default: 'US' 'time' => 'now 1-d', // Default: 'now 1-d' 'hl' => 'en-US', // Default: 'en-US' ]); // Returns: ['data' => ['topics' => [...], 'queries' => [...]]] or ['error' => GoogleTrendsError] if (isset($result['data'])) { echo "Related Topics:\n"; foreach ($result['data']['topics'] as $topic) { echo " - " . $topic['topic']['title'] . "\n"; } echo "\nRelated Queries:\n"; foreach ($result['data']['queries'] as $query) { echo " - " . $query['query'] . "\n"; } }
Error Handling
The library uses custom exception classes for different error types:
use Maximeb97\GoogleTrends\Errors\RateLimitError; use Maximeb97\GoogleTrends\Errors\InvalidRequestError; use Maximeb97\GoogleTrends\Errors\NetworkError; use Maximeb97\GoogleTrends\Errors\ParseError; use Maximeb97\GoogleTrends\Errors\UnknownError; $result = GoogleTrends::dailyTrends(['geo' => 'US']); if (isset($result['error'])) { $error = $result['error']; if ($error instanceof RateLimitError) { echo "Rate limit exceeded. Please try again later.\n"; } elseif ($error instanceof NetworkError) { echo "Network error: " . $error->getMessage() . "\n"; } elseif ($error instanceof ParseError) { echo "Failed to parse response: " . $error->getMessage() . "\n"; } else { echo "Error: " . $error->getMessage() . "\n"; } // Get error code echo "Error code: " . $error->getErrorCode() . "\n"; // Get status code (if available) if ($error->getStatusCode()) { echo "HTTP status: " . $error->getStatusCode() . "\n"; } }
Object-Oriented Usage
You can also use the GoogleTrendsApi class directly:
use Maximeb97\GoogleTrends\GoogleTrendsApi; $api = new GoogleTrendsApi(); $result = $api->dailyTrends(['geo' => 'US']); if (isset($result['data'])) { // Process data }
Data Structures
TrendingStory
class TrendingStory { public string $title; public string $traffic; public ?TrendingImage $image; public array $articles; // TrendingArticle[] public string $shareUrl; }
TrendingArticle
class TrendingArticle { public string $title; public string $url; public string $source; public string $time; public string $snippet; }
TrendingImage
class TrendingImage { public string $newsUrl; public string $source; public string $imageUrl; }
Time Formats
The library supports various time formats for the time parameter:
'now 1-d'- Past day'now 7-d'- Past 7 days'today 1-m'- Past month'today 3-m'- Past 3 months'today 12-m'- Past 12 months'today 5-y'- Past 5 years'all'- All available data (2004-present)- Custom:
'YYYY-MM-DD YYYY-MM-DD'- Specific date range
License
MIT
Credits
This is a PHP port of @shaivpidadi/trends-js.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
maximeb97/trends-php 适用场景与选型建议
maximeb97/trends-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.16k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「api」 「google-trends」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 maximeb97/trends-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 maximeb97/trends-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 maximeb97/trends-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Google Trends API for PHP
A PSR-7 compatible library for making CRUD API endpoints
Alfabank REST API integration
Google Trends API for PHP
Google Trends API for PHP
Zero-dependency raw PHP DNS resolver, domain-ownership verification, and intoDNS/MxToolbox-style diagnostics. Queries authoritative nameservers directly over sockets — never trusts the recursive cache for ownership checks.
统计信息
- 总下载量: 1.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-31