-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "shweshi/opengraph", | ||
"description": "A Laravel package to fetch website Open Graph metadata.", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Shashi Prakash Gautam", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": {}, | ||
|
||
"autoload": { | ||
"psr-4": { | ||
"App\\": "app/", | ||
"shweshi\\OpenGraph\\": "packages/shweshi/OpenGraph/src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace shweshi\OpenGraph\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class OpenGraphFacade extends Facade { | ||
|
||
protected static function getFacadeAccessor() { | ||
return 'OpenGraph'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace shweshi\OpenGraph; | ||
|
||
use DOMDocument; | ||
|
||
class OpenGraph { | ||
|
||
public function fetch($url) { | ||
$html = $this->curl_get_contents($url); | ||
|
||
/** | ||
* parsing starts here: | ||
*/ | ||
$doc = new DOMDocument(); | ||
@$doc->loadHTML($html); | ||
|
||
|
||
$tags = $doc->getElementsByTagName('meta'); | ||
$metadata = array(); | ||
|
||
foreach ($tags as $tag) { | ||
if ($tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) { | ||
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_'); | ||
$value = $tag->getAttribute('content'); | ||
} | ||
if (!empty($key)) { | ||
$metadata[$key] = $value; | ||
} | ||
} | ||
|
||
return $metadata; | ||
} | ||
|
||
protected function curl_get_contents($url) { | ||
$curl = curl_init($url); | ||
curl_setopt($curl, CURLOPT_FAILONERROR, 1); | ||
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); | ||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); | ||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); | ||
curl_setopt($curl, CURLOPT_TIMEOUT, 30); | ||
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); | ||
$response = curl_exec($curl); | ||
curl_close($curl); | ||
return $response; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace shweshi\OpenGraph\Providers; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class OpenGraphProvider extends ServiceProvider { | ||
|
||
/** | ||
* Bootstrap the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() { | ||
// | ||
} | ||
|
||
/** | ||
* Register the application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() { | ||
$this->app->bind('OpenGraph', function() { | ||
return new \shweshi\OpenGraph\OpenGraph; | ||
}); | ||
} | ||
|
||
} |