Skip to content

Commit a5daebb

Browse files
committed
Added code to fetch open graph data
1 parent cbb899e commit a5daebb

5 files changed

Lines changed: 111 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "shweshi/opengraph",
3+
"description": "A Laravel package to fetch website Open Graph metadata.",
4+
"type": "library",
5+
"authors": [
6+
{
7+
"name": "Shashi Prakash Gautam",
8+
"email": "contactmespg@gmail.com"
9+
}
10+
],
11+
"require": {},
12+
13+
"autoload": {
14+
"psr-4": {
15+
"App\\": "app/",
16+
"shweshi\\OpenGraph\\": "packages/shweshi/OpenGraph/src"
17+
}
18+
}
19+
}

src/Facades/OpenGraphFacade.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace shweshi\OpenGraph\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class OpenGraphFacade extends Facade {
8+
9+
protected static function getFacadeAccessor() {
10+
return 'OpenGraph';
11+
}
12+
13+
}

src/OpenGraph.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace shweshi\OpenGraph;
4+
5+
use DOMDocument;
6+
7+
class OpenGraph {
8+
9+
public function fetch($url) {
10+
$html = $this->curl_get_contents($url);
11+
12+
/**
13+
* parsing starts here:
14+
*/
15+
$doc = new DOMDocument();
16+
@$doc->loadHTML($html);
17+
18+
19+
$tags = $doc->getElementsByTagName('meta');
20+
$metadata = array();
21+
22+
foreach ($tags as $tag) {
23+
if ($tag->hasAttribute('property') && strpos($tag->getAttribute('property'), 'og:') === 0) {
24+
$key = strtr(substr($tag->getAttribute('property'), 3), '-', '_');
25+
$value = $tag->getAttribute('content');
26+
}
27+
if (!empty($key)) {
28+
$metadata[$key] = $value;
29+
}
30+
}
31+
32+
return $metadata;
33+
}
34+
35+
protected function curl_get_contents($url) {
36+
$curl = curl_init($url);
37+
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
38+
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
39+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
40+
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
41+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
42+
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
43+
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
44+
$response = curl_exec($curl);
45+
curl_close($curl);
46+
return $response;
47+
}
48+
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace shweshi\OpenGraph\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class OpenGraphProvider extends ServiceProvider {
8+
9+
/**
10+
* Bootstrap the application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot() {
15+
//
16+
}
17+
18+
/**
19+
* Register the application services.
20+
*
21+
* @return void
22+
*/
23+
public function register() {
24+
$this->app->bind('OpenGraph', function() {
25+
return new \shweshi\OpenGraph\OpenGraph;
26+
});
27+
}
28+
29+
}

0 commit comments

Comments
 (0)