diff --git a/README.md b/README.md index 6b84375..14026e7 100644 --- a/README.md +++ b/README.md @@ -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 +category('technology')->language('en')->getTopHeadlines(); +// Returns a PHP Object parsed through the JSON response. +var_dump($request); +``` + *** ## Developers... we need you! diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f7c0f7e --- /dev/null +++ b/composer.json @@ -0,0 +1,9 @@ +{ + "name": "news-api/news-api", + "type": "library", + "license": "MIT", + "require": {}, + "psr-4": { + "NewsAPI\\NewsAPI\\": "src/" + } +} diff --git a/src/src.php b/src/src.php new file mode 100644 index 0000000..bdee71a --- /dev/null +++ b/src/src.php @@ -0,0 +1,120 @@ +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)); + } +} + +?>