Skip to content

Commit

Permalink
Added code to fetch open graph data
Browse files Browse the repository at this point in the history
  • Loading branch information
shweshi committed Jan 10, 2018
1 parent cbb899e commit a5daebb
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
19 changes: 19 additions & 0 deletions composer.json
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"
}
}
}
13 changes: 13 additions & 0 deletions src/Facades/OpenGraphFacade.php
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';
}

}
49 changes: 49 additions & 0 deletions src/OpenGraph.php
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;
}

}
29 changes: 29 additions & 0 deletions src/Providers/OpenGraphProvider.php
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;
});
}

}

0 comments on commit a5daebb

Please sign in to comment.