Coder Social home page Coder Social logo

newsapi's Introduction

๐Ÿ—ž PHP wrapper for NewsAPI.org

travis-build codecov Codacy Badge

Getting Started

Implementing the NewsAPI within your PHP application has never been smoother. The principle behind its design is simplicity without forfeiting power or flexibility.

Installation

Add it to your project via Composer.

composer require gfargo/newsapi

Dependencies

This wrapper utilizes the Requests library from Ryan McCue.

Official website & documentation can be found here.

Configuration

Once included in your project, setup should be very straightforward. Only requirement is a valid API key provided by NewsAPI.org.

Usage

Step 1. Set Access Token

\NewsAPI\Client::setAccessToken('276537c6a3824cdd9eae393c024ff732');

Step 2. Setup Query

Making requests to the API is done via query method, which accepts three parameters. Below are a few examples.

Query parameters:

  • (string) $endpoint Target endpoint. Options are top, everything, and sources.
  • (array) $query_params Query parameters passed to NewsAPI.org
  • (array) $request_options Options passed to Requests library to control CURL.

Examples:

// All articles featuring the keyword 'Open Source'
$request = NewsAPI\Client::query( 'everything', [ 'q' => 'Open Source' ] );
// Top headlines for articles featuring the keyword 'Technology'
$request = NewsAPI\Client::query( 'top', [ 'q' => 'Technology' ] );
// Top articles from 'Business' category
$request = NewsAPI\Client::query( 'top', [ 'category' => 'business' ] );

Step 3. Handling Responses

Each query returns a Request_Response object, more on this here.

In short the Requests library makes dealing with the API responses much easier.

$request = NewsAPI\Client::query( 'top', [ 'category' => 'business' ] );

$request->status_code             // int(200)
$request->headers['content-type'] // string(31) "application/json; charset=utf-8"
$request->url                     // string(54) "https://newsapi.org/v2/top-headlines?category=business"
$request->body                    // string(14385) "{...}"

Change log

Automated release notes can be found here โ†’

newsapi's People

Watchers

 avatar  avatar  avatar

newsapi's Issues

Resolve Issues with Typecasting in Adapter

@gfargo Static analysis by @phpstan report.

$ phpstan analyze src/ -l max

 ------ -----------------------------------------------------------------------------------------------------------------
  Line   v2/Adapter.php
 ------ -----------------------------------------------------------------------------------------------------------------
  29     Variable $apiKey in isset() always exists and is not nullable.
  49     Method NewsAPI\V2\Adapter::__construct() with return type void returns $this(NewsAPI\V2\Adapter) but should not
         return anything.
  72     Cannot cast array|null to string.
 ------ -----------------------------------------------------------------------------------------------------------------

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.