Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# News API SDK for PHP
Coming soon... this is where our officially supported SDK for PHP is going to live.

## Example
```php
<?php
include 'vendor/autoload.php';
$newsapi = new NewsAPI\NewsAPI('your_api_key_here');
$request = $newsapi->category('technology')->language('en')->getTopHeadlines();
// Returns a PHP Object parsed through the JSON response.
var_dump($request);
```

***

## Developers... we need you!
Expand Down
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "news-api/news-api",
"type": "library",
"license": "MIT",
"require": {},
"psr-4": {
"NewsAPI\\NewsAPI\\": "src/"
}
}
120 changes: 120 additions & 0 deletions src/src.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace NewsAPI;

class NewsAPI {
protected $APIKey, $category, $language, $country, $q, $sources, $domains, $from, $to, $sortBy, $page;
public function __construct($APIKey = null) {
$this->APIKey = $APIKey;
}
public function setAPIKey($APIKey) {
$this->APIKey = $APIKey;
return $this;
}
public function category($category) {
$this->category = $category;
return $this;
}
public function language($language) {
$this->language = $language;
return $this;
}
public function country($country) {
$this->country = $country;
return $this;
}
public function q($q) {
$this->q = $q;
return $this;
}
public function sources($sources) {
$this->sources = $sources;
return $this;
}
public function domains($domains) {
$this->domains = $domains;
return $this;
}
public function from($from) {
$this->from = $from;
return $this;
}
public function to($to) {
$this->to = $to;
return $this;
}
public function sortBy($sortBy) {
$this->sortBy = $sortBy;
return $this;
}
public function page($page) {
$this->page = $page;
return $this;
}
public function getTopHeadlines($arr = null) {
if ($arr != null) {
$body = $arr;
}
else {
$body = [
'q' => $this->q,
'sources' => $this->sources,
'category' => $this->category,
'language' => $this->language,
'country' => $this->country
];
}
$url = 'https://newsapi.org/v2/top-headlines' . "?" . http_build_query($body);
return $this->sendRequest($url);
}
public function getEverything($arr = null) {
if ($arr != null) {
$body = $arr;
}
else {
$body = [
'q' => $this->q,
'sources' => $this->sources,
'domains' => $this->domains,
'from' => $this->from,
'to' => $this->to,
'language' => $this->language,
'sortBy' => $this->sortBy,
'page' => $this->page,
];
}
$url = 'https://newsapi.org/v2/everything' . "?" . http_build_query($body);
return $this->sendRequest($url);
}
public function getSources($arr = null) {
if ($arr != null) {
$body = $arr;
}
else {
$body = [
'country' => $this->country,
'language' => $this->language,
'category' => $this->category
];
}
$url = 'https://newsapi.org/v2/sources' . "?" . http_build_query($body);
return $this->sendRequest($url);
}
private function sendRequest($url) {
$cu = curl_init();
$curlArgs = [
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => [
"Authorization: ".$this->APIKey
],
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0
];
curl_setopt_array($cu, $curlArgs);
return json_decode(curl_exec($cu));
}
}

?>