Skip to content

Commit 439608d

Browse files
authored
Merge pull request #28 from shweshi/feature-verify-image-url
Verify image url in metatags.
2 parents 429a421 + 19f81c1 commit 439608d

3 files changed

Lines changed: 81 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
All notable changes to `OpenGraph` will be documented in this file
44

5+
## 1.0.4 - 2019-04-26
6+
7+
- Verify that the image url is valid in the metadata.
8+
59
## 1.0.3 - 2019-01-04
10+
611
- Added support to fetch all metadata
712

813
## 1.0.2 - 2018-11-23
14+
915
- Added tests and autoload for laravel >= 5.5
1016

1117
## 1.0.1 - 2018-01-17
18+
1219
- Fix autoload
1320

1421
## 1.0.0 - 2018-01-17
22+
1523
- initial release

README.md

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
1-
# Laravel OpenGraph
1+
<p align="center">
2+
<a href="https://shweshi.github.io/OpenGraph/">
3+
<img alt="Laravel OpenGraph" src="https://user-images.githubusercontent.com/35127382/56821996-695df500-686d-11e9-9c59-c70d0c78c80e.png" />
4+
</a>
5+
</p>
26

3-
A Laravel package to fetch Open Graph metadata of a website.
7+
<p align="center">
8+
<strong>A Laravel package to fetch Open Graph metadata of a website.</strong>
9+
</p>
410

5-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/shweshi/OpenGraph.svg?style=flat-square)](https://packagist.org/packages/shweshi/OpenGraph)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/shweshi/OpenGraph.svg?style=flat-square)](https://packagist.org/packages/shweshi/OpenGraph)
7-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/shweshi/OpenGraph/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/shweshi/OpenGraph/?branch=master)
8-
[![StyleCI](https://styleci.io/repos/116995669/shield?branch=master)](https://styleci.io/repos/116995669)
9-
[![Build Status](https://scrutinizer-ci.com/g/shweshi/OpenGraph/badges/build.png?b=master)](https://scrutinizer-ci.com/g/shweshi/OpenGraph/build-status/master)
10-
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fshweshi%2FOpenGraph.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fshweshi%2FOpenGraph?ref=badge_shield)
11+
<p align="center">
12+
<a href="https://packagist.org/packages/shweshi/OpenGraph">
13+
<img src="https://img.shields.io/packagist/v/shweshi/OpenGraph.svg?style=flat-square" alt="Latest Version on Packagist">
14+
</a>
1115

12-
## Installation
16+
<a href="https://packagist.org/packages/shweshi/OpenGraph">
17+
<img src="https://img.shields.io/packagist/dt/shweshi/OpenGraph.svg?style=flat-square" alt="Total Downloads">
18+
</a>
19+
20+
<a href="https://scrutinizer-ci.com/g/shweshi/OpenGraph/?branch=master">
21+
<img src="https://scrutinizer-ci.com/g/shweshi/OpenGraph/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality">
22+
</a>
23+
24+
<a href="https://styleci.io/repos/116995669">
25+
<img src="https://styleci.io/repos/116995669/shield?branch=master" alt="StyleCI">
26+
</a>
27+
28+
<a href="https://scrutinizer-ci.com/g/shweshi/OpenGraph/build-status/master">
29+
<img src="https://scrutinizer-ci.com/g/shweshi/OpenGraph/badges/build.png?b=master" alt="Build Status">
30+
</a>
31+
32+
<a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fshweshi%2FOpenGraph?ref=badge_shield">
33+
<img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fshweshi%2FOpenGraph.svg?type=shield" alt="FOSSA Status">
34+
</a>
35+
</p>
36+
37+
## Features
38+
39+
- **Easily fetch metadata of a url.** Laravel OpenGraph fetch all the metadata of a URL.
40+
41+
- **Supports twitter metadata.** Laravel OpenGraph supports twitter OG data too.
42+
43+
- **Verify image URL.** Laravel OpenGraph verifies that the image url in the image metadata is valid or not.
44+
45+
## How to use Laravel OpenGraph
46+
47+
Article can be found on medium blog: https://hackernoon.com/how-to-fetch-open-graph-metadata-in-laravel-2d5d674904d7
48+
49+
### Installation
1350

1451
Perform the following operations in order to use this package
1552

@@ -42,11 +79,11 @@ If you do not run Laravel 5.5 (or higher), then add the service provider in conf
4279

4380
If you do run the package on Laravel 5.5+, package auto-discovery takes care of the magic of adding the service provider.
4481

45-
## Requirements
82+
### Requirements
4683

4784
- You need to install the [DOM](http://www.php.net/en/dom) extension.
4885

49-
## How to use
86+
### How to use
5087

5188
- After following the above steps,
5289

@@ -55,6 +92,7 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of
5592
5693
$data = OpenGraph::fetch("https://unsplash.com/");
5794
```
95+
5896
this will give you an array like this..
5997

6098
```
@@ -67,15 +105,15 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of
67105
'image:secure_url' => 'https://images.unsplash.com/photo-1542841791-1925b02a2bbb?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9&s=aceabe8a2fd1a273da24e68c21768de0',
68106
)
69107
```
70-
108+
71109
You can also pass an optional parameter either true or false along with url. When set false it will only fetch basic metadata and in case of true it will fetch all the other optional metadata as well like audio, video, music and twitter metatags as well.
72-
110+
73111
```
74112
$data = OpenGraph::fetch("https://unsplash.com/", true);
75113
```
76-
114+
77115
this will give you an array like this..
78-
116+
79117
```
80118
array (
81119
'charset' => 'UTF8',
@@ -104,23 +142,22 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of
104142
)
105143
```
106144

107-
## Testing
145+
### Testing
108146

109147
composer test
110148

111-
## Changelog
149+
### Changelog
112150

113151
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
114152

115-
## Contributing
153+
### Contributing
116154

117155
Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE OF CONDUCT](CODE_OF_CONDUCT.md) for details.
118156

119-
## License
157+
### License
120158

121159
The MIT License (MIT). Please see [License File](LICENSE) for more information.
122160

123-
124161
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fshweshi%2FOpenGraph.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fshweshi%2FOpenGraph?ref=badge_large)
125162

126163
### Happy coding!

src/OpenGraph.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public function fetch($url, $allMeta = null)
3131
if (!empty($key)) {
3232
$metadata[$key] = $value;
3333
}
34+
/*
35+
* Verify image url
36+
*/
37+
if (isset($metadata['image'])) {
38+
$isValidImageUrl = $this->verify_image_url($metadata['image']);
39+
if (!$isValidImageUrl) {
40+
$metadata['image'] = '';
41+
}
42+
}
3443
}
3544

3645
return $metadata;
@@ -51,4 +60,11 @@ protected function curl_get_contents($url)
5160

5261
return $response;
5362
}
63+
64+
protected function verify_image_url($url)
65+
{
66+
$headers = get_headers($url);
67+
68+
return stripos($headers[0], '200 OK') ? true : false;
69+
}
5470
}

0 commit comments

Comments
 (0)