Skip to content

Commit 0b68e1e

Browse files
authored
Merge pull request #31 from shweshi/fix-issue-29
Fix issue 29:Package not working on all website
2 parents 439608d + 32e9da6 commit 0b68e1e

3 files changed

Lines changed: 63 additions & 13 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@
3838

3939
- **Easily fetch metadata of a url.** Laravel OpenGraph fetch all the metadata of a URL.
4040

41+
- **Supports language specific metadata.** Laravel OpenGraph can fetch metadata in a specific language if webpage supports.
42+
4143
- **Supports twitter metadata.** Laravel OpenGraph supports twitter OG data too.
4244

4345
- **Verify image URL.** Laravel OpenGraph verifies that the image url in the image metadata is valid or not.
4446

47+
## Demo
48+
49+
```
50+
curl https://laravelopengraph.herokuapp.com/api/fetch?url=ogp.me&allMeta=true&language=en_GB
51+
```
52+
4553
## How to use Laravel OpenGraph
4654

4755
Article can be found on medium blog: https://hackernoon.com/how-to-fetch-open-graph-metadata-in-laravel-2d5d674904d7
@@ -142,6 +150,15 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of
142150
)
143151
```
144152

153+
To fetch the metadata in a specific language you can pass the language as the third argument, this value will be used as the Accept-Language header.
154+
155+
```
156+
$url = "https://ogp.me",
157+
$allMeta = true, // can be false
158+
$language = 'en' // en-US,en;q=0.8,en-GB;q=0.6,es;q=0.4
159+
$data = OpenGraph::fetch($url, $allMeta, $language);
160+
```
161+
145162
### Testing
146163

147164
composer test

src/OpenGraph.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
class OpenGraph
88
{
9-
public function fetch($url, $allMeta = null)
9+
public function fetch($url, $allMeta = null, $lang = null)
1010
{
11-
$html = $this->curl_get_contents($url);
12-
11+
$html = $this->curl_get_contents($url, $lang);
1312
/**
1413
* parsing starts here:.
1514
*/
@@ -45,16 +44,35 @@ public function fetch($url, $allMeta = null)
4544
return $metadata;
4645
}
4746

48-
protected function curl_get_contents($url)
47+
protected function curl_get_contents($url, $lang)
4948
{
50-
$curl = curl_init($url);
51-
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
52-
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
53-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
54-
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
55-
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
56-
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
57-
curl_setopt($curl, CURLOPT_ENCODING, 'UTF-8');
49+
$headers = [
50+
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
51+
'Cache-Control: no-cache',
52+
'User-Agent: Curl',
53+
];
54+
55+
if ($lang) {
56+
array_push($headers, 'Accept-Language: '.$lang);
57+
}
58+
59+
$curl = curl_init();
60+
61+
curl_setopt_array($curl, [
62+
CURLOPT_URL => $url,
63+
CURLOPT_FAILONERROR => false,
64+
CURLOPT_FOLLOWLOCATION => true,
65+
CURLOPT_RETURNTRANSFER => true,
66+
CURLOPT_SSL_VERIFYHOST => false,
67+
CURLOPT_SSL_VERIFYPEER => false,
68+
CURLOPT_ENCODING => 'UTF-8',
69+
CURLOPT_MAXREDIRS => 10,
70+
CURLOPT_TIMEOUT => 30,
71+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
72+
CURLOPT_CUSTOMREQUEST => 'GET',
73+
CURLOPT_HTTPHEADER => $headers,
74+
]);
75+
5876
$response = curl_exec($curl);
5977
curl_close($curl);
6078

tests/OpenGraphTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testFetch()
1212
{
1313
$opengraph = new OpenGraph();
1414
$data = $opengraph->fetch(
15-
'https://www.unsplash.com/'
15+
'https://www.ogp.me/'
1616
);
1717
$this->assertArrayHasKey('title', $data);
1818
$this->assertArrayHasKey('description', $data);
@@ -21,6 +21,21 @@ public function testFetch()
2121
$this->assertArrayHasKey('image', $data);
2222
}
2323

24+
/** @test */
25+
public function testFetchAllMetadata()
26+
{
27+
$opengraph = new OpenGraph();
28+
$data = $opengraph->fetch(
29+
'https://www.ogp.me/', true
30+
);
31+
$this->assertArrayHasKey('title', $data);
32+
$this->assertArrayHasKey('description', $data);
33+
$this->assertArrayHasKey('type', $data);
34+
$this->assertArrayHasKey('url', $data);
35+
$this->assertArrayHasKey('image', $data);
36+
$this->assertArrayHasKey('fb:app_id', $data);
37+
}
38+
2439
/** @test */
2540
public function testFetchReturnsEmptyArrayForWebsiteWithNoMetadata()
2641
{

0 commit comments

Comments
 (0)