Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using PHP library to fetch opengraph
- Loading branch information
Showing
6 changed files
with
56 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Datlechin\LinkPreview\Api\Controllers; | ||
|
||
use Laminas\Diactoros\Response\JsonResponse; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class ScrapperController implements RequestHandlerInterface | ||
{ | ||
protected $web; | ||
|
||
public function __construct(\spekulatius\phpscraper $web) | ||
{ | ||
$this->web = $web; | ||
} | ||
|
||
public function handle(Request $request): Response | ||
{ | ||
$url = isset($request->getQueryParams()['url']) ? $request->getQueryParams()['url'] : ''; | ||
|
||
if (filter_var($url, FILTER_VALIDATE_URL) === false) { | ||
return new JsonResponse([ | ||
'error' => 'Invalid URL', | ||
], 400); | ||
} | ||
|
||
$this->web->go($url); | ||
|
||
return new JsonResponse([ | ||
'site_name' => $this->web->openGraph['og:site_name'] ?? null, | ||
'title' => $this->web->title, | ||
'description' => $this->web->description, | ||
'image' => $this->web->openGraph['og:image'] ?? null, | ||
]); | ||
} | ||
} |