From 0cede23123831e97c3c40b4c08924a9581111447 Mon Sep 17 00:00:00 2001 From: Philly Tan Date: Thu, 14 Dec 2017 15:28:55 +0800 Subject: [PATCH 1/5] Added function for obtaining Sources --- composer.json | 6 ++++++ src.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 composer.json create mode 100644 src.php diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2d58565 --- /dev/null +++ b/composer.json @@ -0,0 +1,6 @@ +{ + "name": "news-api/news-api", + "type": "library", + "license": "MIT", + "require": {} +} diff --git a/src.php b/src.php new file mode 100644 index 0000000..3bc27db --- /dev/null +++ b/src.php @@ -0,0 +1,43 @@ +APIKey = $APIKey; + } + public function setAPIKey($APIKey) { + $this->APIKey = $APIKey; + } + 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 sources() { + $this->cu = curl_init(); + $curlArgs = [ + CURLOPT_URL => 'https://newsapi.org/v2/sources', + CURLOPT_HTTPHEADER => array( + "Authorization: ".$this->APIKey + ), + CURLOPT_RETURNTRANSFER => 1, + CURLOPT_CONNECTTIMEOUT => 10, + CURLOPT_SSL_VERIFYPEER => 0, + CURLOPT_SSL_VERIFYHOST => 0 + ]; + curl_setopt_array($this->cu, $curlArgs); + return curl_exec($this->cu); + } +} + +?> From 3a60f8978e9ab3ed580784712135c67c9a60adc2 Mon Sep 17 00:00:00 2001 From: Philly Tan Date: Thu, 14 Dec 2017 15:45:45 +0800 Subject: [PATCH 2/5] Bug Fix --- src.php | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src.php b/src.php index 3bc27db..403f168 100644 --- a/src.php +++ b/src.php @@ -4,12 +4,12 @@ class NewsAPI { protected $APIKey, $category, $language, $country; - private $cu; public function __construct($APIKey = null) { $this->APIKey = $APIKey; } public function setAPIKey($APIKey) { $this->APIKey = $APIKey; + return $this; } public function category($category) { $this->category = $category; @@ -23,20 +23,34 @@ public function country($country) { $this->country = $country; return $this; } - public function sources() { - $this->cu = curl_init(); + public function sources($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 => 'https://newsapi.org/v2/sources', - CURLOPT_HTTPHEADER => array( + 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($this->cu, $curlArgs); - return curl_exec($this->cu); + curl_setopt_array($cu, $curlArgs); + return curl_exec($cu); } } From 522781dfdbadd3ac0b343a3c641341de3352802c Mon Sep 17 00:00:00 2001 From: Philly Tan Date: Thu, 14 Dec 2017 15:54:31 +0800 Subject: [PATCH 3/5] Added Top Headlines and Everything functions --- src.php | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/src.php b/src.php index 403f168..7bfd04a 100644 --- a/src.php +++ b/src.php @@ -3,7 +3,7 @@ namespace NewsAPI; class NewsAPI { - protected $APIKey, $category, $language, $country; + protected $APIKey, $category, $language, $country, $q, $sources, $domains, $from, $to, $sortBy, $page; public function __construct($APIKey = null) { $this->APIKey = $APIKey; } @@ -23,7 +23,70 @@ public function country($country) { $this->country = $country; return $this; } - public function sources($arr = null) { + 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; } From 794f066a1e77c04afb6f490f03a395fa32dda8e4 Mon Sep 17 00:00:00 2001 From: Philly Tan Date: Thu, 14 Dec 2017 15:59:26 +0800 Subject: [PATCH 4/5] Edited README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 6b84375..9ba76e6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ # News API SDK for PHP Coming soon... this is where our officially supported SDK for PHP is going to live. +## Example +```php +category('technology')->language('en')->getTopHeadlines(); +``` + *** ## Developers... we need you! From 3e5d54544f21821c4b9121094a984da5a6163cf9 Mon Sep 17 00:00:00 2001 From: Philly Tan Date: Thu, 14 Dec 2017 19:48:53 +0800 Subject: [PATCH 5/5] Bug Fix --- README.md | 4 +++- composer.json | 5 ++++- src.php => src/src.php | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) rename src.php => src/src.php (98%) diff --git a/README.md b/README.md index 9ba76e6..14026e7 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,10 @@ Coming soon... this is where our officially supported SDK for PHP is going to li ```php category('technology')->language('en')->getTopHeadlines(); +// Returns a PHP Object parsed through the JSON response. +var_dump($request); ``` *** diff --git a/composer.json b/composer.json index 2d58565..f7c0f7e 100644 --- a/composer.json +++ b/composer.json @@ -2,5 +2,8 @@ "name": "news-api/news-api", "type": "library", "license": "MIT", - "require": {} + "require": {}, + "psr-4": { + "NewsAPI\\NewsAPI\\": "src/" + } } diff --git a/src.php b/src/src.php similarity index 98% rename from src.php rename to src/src.php index 7bfd04a..bdee71a 100644 --- a/src.php +++ b/src/src.php @@ -113,7 +113,7 @@ private function sendRequest($url) { CURLOPT_SSL_VERIFYHOST => 0 ]; curl_setopt_array($cu, $curlArgs); - return curl_exec($cu); + return json_decode(curl_exec($cu)); } }