Skip to content

Commit

Permalink
counting pinterest shares for a url
Browse files Browse the repository at this point in the history
  • Loading branch information
sagautam5 committed Sep 19, 2020
1 parent 96cc820 commit c147e19
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Now, Clear the configuration cache.
php artisan config:cache
```

#### Pinterest

Currently, you don't need any application id or secret to count pinterest shares. You can just call the function.

## Basic Usage

After installation, you can use the share count feature like this:
Expand All @@ -53,8 +57,12 @@ After installation, you can use the share count feature like this:
use Sagautam5\SocialShareCount\ShareCounter;

$url = 'https://www.example.com';
$shareCounts = ShareCounter::getFacebookShares($url);
echo $shareCounts;

$facebookShares = ShareCounter::getFacebookShares($url);
echo $facebookShares;

$pinterestShares = ShareCounter::getPinterestShares($url);
echo $pinterestShares;
```


Expand Down
58 changes: 57 additions & 1 deletion src/ShareCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,32 @@ class ShareCounter
*/
public static function getFacebookShares($url)
{
/**
* setup default counter value
*/
$count = 0;

/**
* Check if credentials are added
*/

throw_if(!(config('share_count.fb_app_id') && config('share_count.fb_app_secret')), new EmptyFacebookCredentialsException('Please add facebook app id and app secret correctly !'));

throw_if(!filter_var($url, FILTER_VALIDATE_URL), new InvalidUrlException('Please enter valid url !',400));
/**
* Check if valid URL is added
*/

throw_if(!filter_var($url, FILTER_VALIDATE_URL), new InvalidUrlException('Please enter valid url including http or https !',400));

/**
* Send get request to facebook server
*/

try{
/**
* Hit Facebook API
*/

$client = new Client();
$response = $client->get('https://graph.facebook.com/v3.0', [
'query' =>
Expand All @@ -41,6 +60,9 @@ public static function getFacebookShares($url)

if($response->getStatusCode() == 200)
{
/**
* Format the data
*/
$data = json_decode($response->getBody());

$count = $data->engagement->share_count;
Expand All @@ -52,4 +74,38 @@ public static function getFacebookShares($url)

return $count;
}

/**
* @param $url
* @return mixed
* @throws \Throwable
*/
public static function getPinterestShares($url)
{
/**
* Check if valid URL is added
*/
throw_if(!filter_var($url, FILTER_VALIDATE_URL), new InvalidUrlException('Please enter valid url http or https !',400));

/**
* Hit Pinterest API
*/
$response = file_get_contents('https://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url='.$url);

/**
* Format the response
*/
if($response[0] !== '[' && $response[0] !== '{') {
$response = substr($response, strpos($response, '('));
}

$response = json_decode(trim($response,'();'), false);

/**
* Get Counts
*/
$count = $response->count;

return $count;
}
}

0 comments on commit c147e19

Please sign in to comment.