Skip to content

Commit

Permalink
Merge pull request #68 from huulbaek/master
Browse files Browse the repository at this point in the history
Add custom user agent
  • Loading branch information
shweshi authored Oct 1, 2020
2 parents 3b811a2 + 85763a0 commit c334ee4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ If you do run the package on Laravel 5.5+, package auto-discovery takes care of
```
$data = OpenGraph::fetch($url, $allMeta, $language, $options);
```
You can use the fifth parameter to set the user-agent ($userAgent) of the request. The default is 'Curl'.
```
$data = OpenGraph::fetch($url, $allMeta, $language, $options, $userAgent);
```

### Exception Handling

Expand Down
8 changes: 4 additions & 4 deletions src/OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class OpenGraph
{
public function fetch($url, $allMeta = null, $lang = null, $options = LIBXML_NOWARNING | LIBXML_NOERROR)
public function fetch($url, $allMeta = null, $lang = null, $options = LIBXML_NOWARNING | LIBXML_NOERROR, $userAgent = 'Curl')
{
$html = $this->curl_get_contents($url, $lang);
$html = $this->curl_get_contents($url, $lang, $userAgent);
/**
* parsing starts here:.
*/
Expand Down Expand Up @@ -54,12 +54,12 @@ public function fetch($url, $allMeta = null, $lang = null, $options = LIBXML_NOW
return $metadata;
}

protected function curl_get_contents($url, $lang)
protected function curl_get_contents($url, $lang, $userAgent)
{
$headers = [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Cache-Control: no-cache',
'User-Agent: Curl',
'User-Agent: '.$userAgent,
];

if ($lang) {
Expand Down

0 comments on commit c334ee4

Please sign in to comment.