Skip to content

Commit

Permalink
Merge pull request #31 from shweshi/fix-issue-29
Browse files Browse the repository at this point in the history
Fix issue 29:Package not working on all website
  • Loading branch information
shweshi authored Jun 12, 2019
2 parents 439608d + 32e9da6 commit 0b68e1e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@

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

- **Supports language specific metadata.** Laravel OpenGraph can fetch metadata in a specific language if webpage supports.

- **Supports twitter metadata.** Laravel OpenGraph supports twitter OG data too.

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

## Demo

```
curl https://laravelopengraph.herokuapp.com/api/fetch?url=ogp.me&allMeta=true&language=en_GB
```

## How to use Laravel OpenGraph

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

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.

```
$url = "https://ogp.me",
$allMeta = true, // can be false
$language = 'en' // en-US,en;q=0.8,en-GB;q=0.6,es;q=0.4
$data = OpenGraph::fetch($url, $allMeta, $language);
```

### Testing

composer test
Expand Down
42 changes: 30 additions & 12 deletions src/OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

class OpenGraph
{
public function fetch($url, $allMeta = null)
public function fetch($url, $allMeta = null, $lang = null)
{
$html = $this->curl_get_contents($url);

$html = $this->curl_get_contents($url, $lang);
/**
* parsing starts here:.
*/
Expand Down Expand Up @@ -45,16 +44,35 @@ public function fetch($url, $allMeta = null)
return $metadata;
}

protected function curl_get_contents($url)
protected function curl_get_contents($url, $lang)
{
$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_ENCODING, 'UTF-8');
$headers = [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Cache-Control: no-cache',
'User-Agent: Curl',
];

if ($lang) {
array_push($headers, 'Accept-Language: '.$lang);
}

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_FAILONERROR => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => 'UTF-8',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => $headers,
]);

$response = curl_exec($curl);
curl_close($curl);

Expand Down
17 changes: 16 additions & 1 deletion tests/OpenGraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testFetch()
{
$opengraph = new OpenGraph();
$data = $opengraph->fetch(
'https://www.unsplash.com/'
'https://www.ogp.me/'
);
$this->assertArrayHasKey('title', $data);
$this->assertArrayHasKey('description', $data);
Expand All @@ -21,6 +21,21 @@ public function testFetch()
$this->assertArrayHasKey('image', $data);
}

/** @test */
public function testFetchAllMetadata()
{
$opengraph = new OpenGraph();
$data = $opengraph->fetch(
'https://www.ogp.me/', true
);
$this->assertArrayHasKey('title', $data);
$this->assertArrayHasKey('description', $data);
$this->assertArrayHasKey('type', $data);
$this->assertArrayHasKey('url', $data);
$this->assertArrayHasKey('image', $data);
$this->assertArrayHasKey('fb:app_id', $data);
}

/** @test */
public function testFetchReturnsEmptyArrayForWebsiteWithNoMetadata()
{
Expand Down

0 comments on commit 0b68e1e

Please sign in to comment.