From 1680f70dffab0162b4673cb5aa01e67cb4fda15c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Youn=C3=A8s=20El=20Biache?= Date: Sun, 16 Aug 2015 12:43:05 +0200 Subject: [PATCH 1/8] namespacing; guzzle; complete refactor --- composer.json | 4 +- src/Socialworth.php | 277 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 src/Socialworth.php diff --git a/composer.json b/composer.json index 6ca0a18..4ee2a7b 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,8 @@ } ], "autoload": { - "psr-0": { - "Evansims\\": "src/" + "psr-4": { + "Evansims\\Socialworth": "src/" } }, "require": { diff --git a/src/Socialworth.php b/src/Socialworth.php new file mode 100644 index 0000000..27a5f07 --- /dev/null +++ b/src/Socialworth.php @@ -0,0 +1,277 @@ + true, + 'facebook' => true, + 'pinterest' => true, + 'reddit' => true, + 'stumbleupon' => true, + 'linkedin' => true, + 'hackernews' => false, + 'testcase' => false, + ]; + + /** + * Constructor + * @param String $url + * @param array $services restrict counter to specific services; ex: ['twitter', 'linkedin'] + */ + public function __construct($url = null, array $services = []) + { + $this->setUrl($url); + + if (!empty($services)) { + $this->services = array_fill_keys(array_keys($this->services), false); + + foreach ($services as $service) { + $this->services[$service] = true; + } + } + } + + public function setUrl($url) + { + if (filter_var($url, FILTER_VALIDATE_URL)) { + $this->url = $url; + + } else { + throw new \InvalidArgumentException(_('The address provided is not a valid URL.')); + } + + return $this; + } + + public function all() + { + $endpoints = $this->apiEndpoints(); + $response = [ 'total' => 0 ]; + + foreach ($this->services as $service => $enabled) { + if ($enabled && isset($endpoints[$service])) { + $actions = $this->__get($endpoints[$service]); + $response[$service] = $actions; + $response['total'] += $actions; + } + } + + return (Object) $response; + } + + public function __set($service, $enabled) + { + if (isset($this->services[$service])) { + $this->services[$service] = (bool) $enabled; + return true; + } + + return false; + } + + public function __get($service) + { + if (is_string($service)) { + $service = strtolower($service); + $endpoints = $this->apiEndpoints(); + + if (isset($endpoints[$service])) { + $service = $endpoints[$service]; + + } else { + throw new \Exception(sprintf(_('Unknown service %s'), $service)); + } + } + + if (!is_array($service) || !isset($service['url'])) { + throw new \InvalidArgumentException(_('Argument expected to be a service name as a string.')); + } + + return $service['callback']($this->apiRequest($service['url'])); + + } + + public function __isset($service) + { + if (isset($this->services[$service])) { + return (bool) $this->services[$service]; + } + + return false; + } + + public function __unset($service) + { + if (isset($this->services[$service])) { + $this->services[$service] = false; + + return true; + } + + return false; + } + + public function __call($service, $arguments = []) + { + $previous_url = $this->url; + + if (isset($arguments[0]) && filter_var($arguments[0], FILTER_VALIDATE_URL)) { + $this->url = $arguments[0]; + } + + $response = $this->__get($service); + $this->url = $previous_url; + return $response; + } + + public static function __callStatic($service, $arguments = []) + { + if (!isset($arguments[0]) || !filter_var($arguments[0], FILTER_VALIDATE_URL)) { + throw new \InvalidArgumentException(_('You must specify an address to query.')); + } + + $instance = new Socialworth($arguments[0]); + return $instance->$service; + } + + protected function apiRequest($endpoint) + { + if ( !isset($this->url)) { + throw new \InvalidArgumentException(_('You must specify an address to query.')); + } + + if ( !$this->client) { + $this->client = new Client(); + } + + $response = $this->client->get(rtrim($endpoint, '?&')); + + if ($response) { + return (strpos($response->getHeader('Content-Type'), 'json') !== false) + ? $response->json() + : $response->getBody()->getContents(); + } + + return false; + } + + protected function apiEndpoints() + { + return [ + 'testcase' => [ + 'url' => 'http://thisisbogus.supercalifragilisticexpialidocious.io', + 'callback' => function($resp) { + return $resp; + } + ], + + 'facebook' => [ + 'url' => 'https://graph.facebook.com/fql?q=' . urlencode("SELECT like_count, total_count, share_count, click_count, comment_count FROM link_stat WHERE url = \"{$this->url}\""), + 'callback' => function($resp) { + $resp = json_decode($resp); + + if ($resp && isset($resp->data[0]->total_count) && $resp->data[0]->total_count) { + return (int)$resp->data[0]->total_count; + } + + return 0; + } + ], + + 'pinterest' => [ + 'url' => 'http://api.pinterest.com/v1/urls/count.json?url='.$this->url, + 'callback' => function($resp) { + if ($resp) { + $resp = json_decode(substr($resp, strpos($resp, '{'), -1)); + + if (isset($resp->count) && $resp->count) { + return (int) $resp->count; + } + } + + return 0; + } + ], + + 'twitter' => [ + 'url' => 'http://cdn.api.twitter.com/1/urls/count.json?url='.$this->url, + 'callback' => function($resp) { + return (isset($resp['count']) && $resp['count']) + ? (int) $resp['count'] + : 0; + } + ], + + 'linkedin' => [ + 'url' => 'http://www.linkedin.com/countserv/count/share?format=json&url='.$this->url, + 'callback' => function($resp) { + return (isset($resp['count']) && $resp['count']) + ? (int) $resp['count'] + : 0; + } + ], + + 'stumbleupon' => [ + 'url' => 'http://www.stumbleupon.com/services/1.01/badge.getinfo?url='.$this->url, + 'callback' => function($resp) { + $resp = json_decode($resp); + + return (isset($resp->result->views) && $resp->result->views) + ? (int) $resp->result->views + : 0; + } + ], + + 'reddit' => [ + 'url' => 'http://www.reddit.com/api/info.json?url='.$this->url, + 'callback' => function($resp) { + + if (isset($resp['data']['children']) && $resp['data']['children']) { + $c = 0; + + foreach ($resp['data']['children'] as $story) { + if (isset($story['data']['ups'])) { + $c = $c + (int) $story['data']['ups']; + } + } + + return (int) $c; + } + + return 0; + } + ], + + 'hackernews' => [ + 'url' => 'http://api.thriftdb.com/api.hnsearch.com/items/_search?q=&filter[fields][url]='.$this->url, + 'callback' => function($resp) { + $resp = json_decode($resp); + + if ($resp && isset($resp->results) && $resp->results) { + $c = 0; + foreach ($resp->results as $story) { + $c++; + if (isset($story->item) && isset($story->item->points)) { + $c = $c + (int)$story->item->points; + } + if (isset($story->item) && isset($story->item->num_comments)) { + $c = $c + (int)$story->item->num_comments; + } + } + return (int)$c; + } + + return 0; + } + ], + ]; + } +} From 6d18a8dbcef10beb6655224d066fc7e93149e0ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Youn=C3=A8s=20El=20Biache?= Date: Sun, 16 Aug 2015 12:45:00 +0200 Subject: [PATCH 2/8] psr4 fix --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 4ee2a7b..c174cee 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "autoload": { "psr-4": { - "Evansims\\Socialworth": "src/" + "Evansims\\Socialworth\\": "src/" } }, "require": { From a9a68842c6f52a17306effe6bbf2545f0b5a672c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Youn=C3=A8s=20El=20Biache?= Date: Sun, 16 Aug 2015 13:02:47 +0200 Subject: [PATCH 3/8] fix; updated documentation --- README.md | 13 +++++-------- demo.php | 2 +- src/Socialworth.php | 17 +++++++++++------ tests/SocialworthTest.php | 3 ++- tests/bootstrap.php | 4 ++-- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a423979..5f30e30 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ A simple PHP library for determining the popularity of a given URL by querying s It presently supports: - Twitter (counts mentions and retweets) - Facebook (counts likes, comments and shares) -- Google+ (+1s) - Pinterest (shares) - Reddit (counts submitted stories and upvotes) - StumbleUpon views @@ -39,7 +38,7 @@ before invoking Socialworth: To query all supported services for a URL: all()); @@ -48,7 +47,7 @@ To query all supported services for a URL: Alternatively you can query just one service: @@ -56,10 +55,9 @@ Alternatively you can query just one service: Or leave out specific services from your query: linkedin = false; + $socialworth = new Socialworth(['linkedin', 'twitter']) // won't do facebook etc. var_dump($socialworth->all()); ?> @@ -68,7 +66,7 @@ The `all()` method will return an object that you can use to grab individual service results or find the combined popularity from the services: all(); @@ -102,7 +100,6 @@ Whether from the CLI or the browser, you will receive a JSON object back. "facebook": 15284, "pinterest": 157, "reddit": 5, - "googleplus": 6049, "stumbleupon": 297, "linkedin": 0 } diff --git a/demo.php b/demo.php index c4da2ac..b5f1853 100644 --- a/demo.php +++ b/demo.php @@ -1,7 +1,7 @@ 1)) { diff --git a/src/Socialworth.php b/src/Socialworth.php index 27a5f07..ad37a30 100644 --- a/src/Socialworth.php +++ b/src/Socialworth.php @@ -28,7 +28,7 @@ class Socialworth */ public function __construct($url = null, array $services = []) { - $this->setUrl($url); + $this->url($url); if (!empty($services)) { $this->services = array_fill_keys(array_keys($this->services), false); @@ -39,13 +39,18 @@ public function __construct($url = null, array $services = []) } } - public function setUrl($url) + public function url($url) { - if (filter_var($url, FILTER_VALIDATE_URL)) { - $this->url = $url; - + if ($url) { + if (filter_var($url, FILTER_VALIDATE_URL)) { + $this->url = $url; + + } else { + throw new \InvalidArgumentException(_('The address provided is not a valid URL.')); + } + } else { - throw new \InvalidArgumentException(_('The address provided is not a valid URL.')); + return $this->url; } return $this; diff --git a/tests/SocialworthTest.php b/tests/SocialworthTest.php index 1efcf66..8c5e44f 100644 --- a/tests/SocialworthTest.php +++ b/tests/SocialworthTest.php @@ -1,7 +1,8 @@ Date: Mon, 17 Aug 2015 18:17:54 +0200 Subject: [PATCH 4/8] cleanup; fixing tests? --- src/Evansims/Socialworth.php | 328 ----------------------------------- tests/SocialworthTest.php | 42 ++--- tests/bootstrap.php | 2 +- 3 files changed, 22 insertions(+), 350 deletions(-) delete mode 100644 src/Evansims/Socialworth.php diff --git a/src/Evansims/Socialworth.php b/src/Evansims/Socialworth.php deleted file mode 100644 index 22e63fb..0000000 --- a/src/Evansims/Socialworth.php +++ /dev/null @@ -1,328 +0,0 @@ - true, - 'facebook' => true, - 'pinterest' => true, - 'reddit' => true, - //'hackernews' => true, - 'googleplus' => true, - 'stumbleupon' => true, - 'linkedin' => true, - 'testcase' => false - ); - - public function __construct($url = null, $services = array()) - { - $this->url($url); - $this->services = array_merge($this->services, $services); - - return $this; - } - - public function url($url = null) - { - if ($url) { - if (filter_var($url, FILTER_VALIDATE_URL)) { - $this->url = $url; - return true; - } else { - throw new \InvalidArgumentException(_('The address provided is not a valid URL.')); - } - } else { - return $this->url; - } - } - - private function apiEndpoints() - { - return array( - 'facebook' => array( - 'method' => 'GET', - 'url' => 'https://graph.facebook.com/fql?q=' . urlencode("SELECT like_count, total_count, share_count, click_count, comment_count FROM link_stat WHERE url = \"{$this->url}\""), - "callback" => function ($resp) { - if ($resp && isset($resp->data[0]->total_count) && $resp->data[0]->total_count) { - return (int)$resp->data[0]->total_count; - } - - return 0; - }), - 'pinterest' => array( - 'method' => 'GET', - 'url' => "http://api.pinterest.com/v1/urls/count.json?url={$this->url}", - "callback" => function ($resp) { - if ($resp) { - $resp = json_decode(substr($resp, strpos($resp, '{'), -1)); - if ($resp && isset($resp->count) && $resp->count) { - return (int)$resp->count; - } - } - - return 0; - }), - 'twitter' => array( - 'method' => 'GET', - 'url' => "http://cdn.api.twitter.com/1/urls/count.json?url={$this->url}", - "callback" => function ($resp) { - if ($resp && isset($resp->count) && $resp->count) { - return (int)$resp->count; - } - - return 0; - }), - 'linkedin' => array( - 'method' => 'GET', - 'url' => "http://www.linkedin.com/countserv/count/share?url={$this->url}&format=json", - "callback" => function ($resp) { - if ($resp && isset($resp->count) && $resp->count) { - return (int)$resp->count; - } - - return 0; - }), - 'stumbleupon' => array( - 'method' => 'GET', - 'url' => "http://www.stumbleupon.com/services/1.01/badge.getinfo?url={$this->url}", - "callback" => function ($resp) { - if ($resp && isset($resp->result) && isset($resp->result->views) && $resp->result->views) { - return (int)$resp->result->views; - } - - return 0; - }), - 'reddit' => array( - 'method' => 'GET', - 'url' => "http://www.reddit.com/api/info.json?url={$this->url}", - "callback" => function ($resp) { - if ($resp && isset($resp->data->children) && $resp->data->children) { - $c = 0; - foreach ($resp->data->children as $story) { - if (isset($story->data) && isset($story->data->ups)) { - $c = $c + (int)$story->data->ups; - } - } - return (int)$c; - } - - return 0; - }), -/* 'hackernews' => array( - 'method' => 'GET', - 'url' => "http://api.thriftdb.com/api.hnsearch.com/items/_search?q=&filter[fields][url]={$this->url}", - "callback" => function ($resp) { - if ($resp && isset($resp->results) && $resp->results) { - $c = 0; - foreach ($resp->results as $story) { - $c++; - if (isset($story->item) && isset($story->item->points)) { - $c = $c + (int)$story->item->points; - } - if (isset($story->item) && isset($story->item->num_comments)) { - $c = $c + (int)$story->item->num_comments; - } - } - return (int)$c; - } - - return 0; - }),*/ - 'googleplus' => array( - 'method' => 'POST', - 'headers' => array('Content-type: application/json'), - 'url' => 'https://clients6.google.com/rpc', - 'payload' => json_encode(array( - 'method' => 'pos.plusones.get', - 'id' => 'p', - 'params' => array( - "nolog" => true, - "id" => $this->url, - "source" => "widget", - "userId" => "@viewer", - "groupId" => "@self" - ), - "jsonrpc" => "2.0", - "key" => "p", - "apiVersion" => "v1" - )), - "callback" => function ($resp) { - if ($resp && isset($resp->result->metadata->globalCounts->count) && $resp->result->metadata->globalCounts->count) { - return (int)$resp->result->metadata->globalCounts->count; - } - - return 0; - }), - 'testcase' => array( - 'method' => 'GET', - 'url' => "http://thisisbogus.supercalifragilisticexpialidocious.io", - "callback" => function ($resp) { - return $resp; - }) - ); - } - - private function apiRequest($method, $endpoint, $params = array()) - { - if (!isset($this->url)) { - throw new \InvalidArgumentException(_('You must specify an address to query.')); - } - - $method = strtoupper($method); - $options = array(); - - if (! $this->curl) { - $this->curl = curl_init(); - } - - if ($method == 'GET') { - $options[CURLOPT_HTTPGET] = true; - } elseif ($method == 'POST') { - $options[CURLOPT_POST] = true; - //} else { - // $options[CURLOPT_CUSTOMREQUEST] = $method; - } - - if (isset($params['headers']) && $params['headers']) { - $options[CURLOPT_HTTPHEADER] = $params['headers']; - } - - if ($method !== 'GET' && isset($params['payload']) && $params['payload']) { - $options[CURLOPT_POSTFIELDS] = $params['payload']; - } - - /* - if ($method === 'GET' && count($params)) { - foreach ($params as $p => $v) { - $endpoint .= $p . '=' . urlencode($v) . '&'; - } - } - */ - - $options[CURLOPT_URL] = rtrim($endpoint, '?&'); - $options[CURLOPT_TIMEOUT] = 5; - $options[CURLOPT_HEADER] = false; - $options[CURLOPT_RETURNTRANSFER] = true; - $options[CURLOPT_SSL_VERIFYPEER] = false; - $options[CURLOPT_FOLLOWLOCATION] = true; - - curl_setopt_array($this->curl, $options); - - if ($raw = curl_exec($this->curl)) { - if ($resp = json_decode($raw)) { - return $resp; - } else { - return $raw; - } - } - - return false; - } - - public function all() - { - $endpoints = $this->apiEndpoints(); - $response = array( - 'total' => 0 - ); - - foreach ($this->services as $service => $enabled) { - if ($enabled && isset($endpoints[$service])) { - $actions = $this->__get($endpoints[$service]); - $response[$service] = $actions; - $response['total'] += $actions; - } - } - - return (object)$response; - } - - public function __set($service, $enabled) - { - if (isset($this->services[$service])) { - $this->services[$service] = (boolean)$enabled; - return true; - } - - return false; - } - - public function __get($service) - { - if (is_string($service)) { - $service = strtolower($service); - $endpoints = $this->apiEndpoints(); - - if (isset($endpoints[$service])) { - $service = $endpoints[$service]; - } else { - throw new \Exception(sprintf(_('Unknown service %s'), $service)); - } - } - - if (!is_array($service) || !isset($service['url'])) { - throw new \InvalidArgumentException(_('Argument expected to be a service name as a string.')); - } - - $params = array(); - - if (isset($service['headers'])) { - $params['headers'] = $service['headers']; - } - - if (isset($service['payload'])) { - $params['payload'] = $service['payload']; - } - - $actions = $service['callback']($this->apiRequest($service['method'], $service['url'], $params)); - return $actions; - } - - public function __isset($service) - { - if (isset($this->services[$service])) { - if ($this->services[$service] === true) { - return true; - } - } - - return false; - } - - public function __unset($service) - { - if (isset($this->services[$service])) { - $this->services[$service] = false; - return true; - } - - return false; - } - - public function __call($service, $arguments = array()) - { - $previous_url = $this->url; - - if (isset($arguments[0]) && filter_var($arguments[0], FILTER_VALIDATE_URL)) { - $this->url = $arguments[0]; - } - - $response = $this->__get($service); - $this->url = $previous_url; - return $response; - } - - public static function __callStatic($service, $arguments = array()) - { - if (!isset($arguments[0]) || !filter_var($arguments[0], FILTER_VALIDATE_URL)) { - throw new \InvalidArgumentException(_('You must specify an address to query.')); - } - - $instance = new Socialworth($arguments[0]); - return $instance->$service; - } -} diff --git a/tests/SocialworthTest.php b/tests/SocialworthTest.php index 8c5e44f..dd62165 100644 --- a/tests/SocialworthTest.php +++ b/tests/SocialworthTest.php @@ -183,27 +183,27 @@ public function testRedditBogus() $this->assertEmpty(Socialworth::reddit($this->test_no_results_url), _("Reddit bogus url test did not return expected response.")); } - public function testHackerNews() - { - $this->markTestSkipped(_("Hacker News API endpoint is offline.")); - //$this->assertNotEmpty(Socialworth::hackernews($this->test_url), _("Hacker News test did not return expected response.")); - } - - public function testHackerNewsBogus() - { - $this->markTestSkipped(_("Hacker News API endpoint is offline.")); - //$this->assertEmpty(Socialworth::hackernews($this->test_no_results_url), _("Hacker News bogus url test did not return expected response.")); - } - - public function testGooglePlus() - { - $this->assertNotEmpty(Socialworth::googleplus($this->test_url), _("Google+ test did not return expected response.")); - } - - public function testGooglePlusBogus() - { - $this->assertEmpty(Socialworth::googleplus($this->test_no_results_url), _("Google+ bogus url test did not return expected response.")); - } + // public function testHackerNews() + // { + // $this->markTestSkipped(_("Hacker News API endpoint is offline.")); + // //$this->assertNotEmpty(Socialworth::hackernews($this->test_url), _("Hacker News test did not return expected response.")); + // } + + // public function testHackerNewsBogus() + // { + // $this->markTestSkipped(_("Hacker News API endpoint is offline.")); + // //$this->assertEmpty(Socialworth::hackernews($this->test_no_results_url), _("Hacker News bogus url test did not return expected response.")); + // } + + // public function testGooglePlus() + // { + // $this->assertNotEmpty(Socialworth::googleplus($this->test_url), _("Google+ test did not return expected response.")); + // } + + // public function testGooglePlusBogus() + // { + // $this->assertEmpty(Socialworth::googleplus($this->test_no_results_url), _("Google+ bogus url test did not return expected response.")); + // } public function testStumbleUpon() { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f2876ca..fe31bc8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,4 +1,4 @@ Date: Mon, 17 Aug 2015 18:21:52 +0200 Subject: [PATCH 5/8] requirements fix --- composer.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c174cee..80ed11b 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,9 @@ } }, "require": { - "php": ">=5.3.0", - "lib-curl": "*" + "php": ">=5.4.0", + "lib-curl": "*", + "guzzlehttp/guzzle": "^5.3" }, "require-dev": { "satooshi/php-coveralls": "dev-master" From d7e645d619f3caef6e535c97eb26d15a3bcfff85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Youn=C3=A8s=20El=20Biache?= Date: Tue, 18 Aug 2015 21:42:37 +0200 Subject: [PATCH 6/8] added setClient() --- README.md | 50 ++++++++++++++++++++++----------------------- src/Socialworth.php | 7 ++++++- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 5f30e30..4db79bc 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ most popular articles for optimizing placement, or featuring social network counters on your pages without relying on bloated external JavaScript includes. ## Installation + To add this package as a dependency for your project, simply add `evansims/socialworth` to your project's composer.json file. Here is an example of a minimal composer.json file: @@ -37,44 +38,43 @@ before invoking Socialworth: ## Usage To query all supported services for a URL: - all()); - ?> +```php +use Evansims\Socialworth\Socialworth; +$socialworth = new Socialworth('https://github.com/'); +var_dump($socialworth->all()); +``` Alternatively you can query just one service: - +```php +var_dump(Socialworth::twitter('https://github.com/')); +``` Or leave out specific services from your query: - all()); - ?> +var_dump($socialworth->all()); +``` The `all()` method will return an object that you can use to grab individual service results or find the combined popularity from the services: - all(); + +var_dump($response->total); // Total likes, shares, upvotes, etc. +var_dump($response->reddit); // Just shares and upvotes from reddit. +var_dump($response->twitter); // Just mentions, retweets and shares on Twitter. +``` - $socialworth = new Socialworth('https://github.com/'); - $response = $socialworth->all(); +The `setClient` allows to set a custom Guzzle Client so you can attach subscribers or mock request - var_dump($response->total); // Total likes, shares, upvotes, etc. - var_dump($response->reddit); // Just shares and upvotes from reddit. - var_dump($response->twitter); // Just mentions, retweets and shares on Twitter. - ?> +```php +$socialworth->setClient(new \GuzzleHttp\Client()); +``` ## Demo Script A demo script is provided that allows you to query the library from your diff --git a/src/Socialworth.php b/src/Socialworth.php index ad37a30..be2379d 100644 --- a/src/Socialworth.php +++ b/src/Socialworth.php @@ -72,6 +72,11 @@ public function all() return (Object) $response; } + public function setClient (Client $client) + { + $this->client = $client; + } + public function __set($service, $enabled) { if (isset($this->services[$service])) { @@ -154,7 +159,7 @@ protected function apiRequest($endpoint) } if ( !$this->client) { - $this->client = new Client(); + $this->setClient(new Client()); } $response = $this->client->get(rtrim($endpoint, '?&')); From 73b5be5126624bb39b9638b779faab38c7baddad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Youn=C3=A8s=20El=20Biache?= Date: Tue, 18 Aug 2015 21:44:41 +0200 Subject: [PATCH 7/8] misc --- src/Socialworth.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Socialworth.php b/src/Socialworth.php index be2379d..1ab8821 100644 --- a/src/Socialworth.php +++ b/src/Socialworth.php @@ -72,7 +72,7 @@ public function all() return (Object) $response; } - public function setClient (Client $client) + public function setClient(Client $client) { $this->client = $client; } From d89672a58ad37aad8eaab8e6b722f69e178a6cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Youn=C3=A8s=20El=20Biache?= Date: Wed, 19 Aug 2015 16:36:22 +0200 Subject: [PATCH 8/8] avoid json parse exception --- src/Socialworth.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Socialworth.php b/src/Socialworth.php index 1ab8821..c38c749 100644 --- a/src/Socialworth.php +++ b/src/Socialworth.php @@ -3,6 +3,7 @@ namespace Evansims\Socialworth; use GuzzleHttp\Client; +use GuzzleHttp\Exception\ParseException; class Socialworth { @@ -164,10 +165,16 @@ protected function apiRequest($endpoint) $response = $this->client->get(rtrim($endpoint, '?&')); - if ($response) { - return (strpos($response->getHeader('Content-Type'), 'json') !== false) - ? $response->json() - : $response->getBody()->getContents(); + try { + if ($response) { + $raw = $response->getBody()->getContents(); + $isJson = strpos($response->getHeader('Content-Type'), 'json') !== false; + + return ($raw and $isJson) ? $response->json() : $raw; + } + + } catch (ParseException $e) { + return $raw; } return false;