gnews-io/gnews-io-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gnews-io/gnews-io-php

最新稳定版本:2.1.0

Composer 安装命令:

composer require gnews-io/gnews-io-php

包简介

A PHP client for GNews API

README 文档

README

A PHP client for GNews API, designed to be simple and easy to use.

Documentation

Installation

composer require gnews-io/gnews-io-php

Usage

Initialization

<?php
require_once 'vendor/autoload.php';

$client = new \GNews\GNews('YOUR_API_KEY');

Search Endpoint

// Search for articles
$articles = $client->search('bitcoin', [
    'lang' => 'en',                   // Optional, languages of articles 
    'country' => 'us',                // Optional, country of origin of the source
    'max' => 10,                      // Optional, maximum number of articles to be returned
    'from' => '2025-01-01T00:00:00Z', // Optional, minimum publication date (included)
    'to' => '2025-12-31T23:59:59Z',   // Optional, maximum publication date (included)
    // ..., any additional parameter specified in the documentation (see https://docs.gnews.io)
]);

echo "---\n";
foreach ($articles as $article) {
    echo $article->getTitle() . "\n";
    echo $article->getSourceName() . "\n";
    echo $article->getUrl() . "\n";
    // ...
    echo "---\n";
}

Top Headlines Endpoint

// Get the top headlines
$articles = $client->getTopHeadlines([
    'category' => 'technology'        // Optional, desired category
    'lang' => 'en',                   // Optional, languages of articles
    'country' => 'us',                // Optional, country of origin of the source
    'max' => 10,                      // Optional, maximum number of articles to be returned
    // ..., any additional parameter specified in the documentation (see https://docs.gnews.io)
]);

echo "---\n";
foreach ($articles as $article) {
    echo $article->getTitle() . "\n";
    echo $article->getSourceName() . "\n";
    echo $article->getUrl() . "\n";
    // ...
    echo "---\n";
}

Response Format

The API results are returned in an ArticleCollection object:

ArticleCollection

Method Description
getTotalArticles() Returns the total number of available articles
getArticles() Returns an array of Article objects
count() Returns the number of articles in the collection
Array access You can access articles with $articles[0]
Iteration You can use foreach($articles as $article)

Article

Method Description
getId() Get the article ID (also accessible via ->id)
getTitle() Get the article title (also accessible via ->title)
getDescription() Get the article description (also accessible via ->description)
getContent() Get the article content (also accessible via ->content)
getUrl() Get the article URL (also accessible via ->url)
getImage() Get the article image URL (also accessible via ->image)
getPublishedAt() Get the publication date (also accessible via ->publishedAt)
getSource() Get the complete source information array (also accessible via ->source)
getSourceId() Get the source ID (also accessible via ->source['id'])
getSourceName() Get the source name (also accessible via ->source['name'])
getSourceUrl() Get the source URL (also accessible via ->source['url'])
getSourceCountry() Get the source country of origin (also accessible via ->source['country'])

Error Handling

The library throws errors in the following cases:

  • Missing API key during initialization
  • Network errors
  • API request timeouts
  • API error responses

统计信息

  • 总下载量: 42
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-08