mirror of
https://github.com/codex-team/editor.js
synced 2024-05-23 08:42:25 +02:00
Link plugin backend (#64)
* Server meta-getter * Add url and url-text parsing * Url parsing * Add timeouts * Add user-agent field
This commit is contained in:
parent
a6a1fec6d8
commit
dc3c4e9a98
96
server/index.php
Normal file
96
server/index.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
function file_get_contents_curl($url)
|
||||
{
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36');
|
||||
|
||||
|
||||
$data = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
function get_url()
|
||||
{
|
||||
if (!isset($_GET['url']))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$url = $_GET['url'];
|
||||
|
||||
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
function get_meta_from_html($html)
|
||||
{
|
||||
$doc = new DOMDocument();
|
||||
@$doc->loadHTML($html);
|
||||
$nodes = $doc->getElementsByTagName('title');
|
||||
|
||||
$title = $nodes->item(0)->nodeValue;
|
||||
$description = "";
|
||||
$keywords = "";
|
||||
$image = "";
|
||||
|
||||
$metas = $doc->getElementsByTagName('meta');
|
||||
|
||||
for ($i = 0; $i < $metas->length; $i++)
|
||||
{
|
||||
$meta = $metas->item($i);
|
||||
if($meta->getAttribute('name') == 'description')
|
||||
$description = $meta->getAttribute('content');
|
||||
if($meta->getAttribute('name') == 'keywords')
|
||||
$keywords = $meta->getAttribute('content');
|
||||
if($meta->getAttribute('property')=='og:image'){
|
||||
$image = $meta->getAttribute('content');
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'image' => $image,
|
||||
'title' => $title,
|
||||
'description' => $description
|
||||
];
|
||||
}
|
||||
|
||||
$url = get_url();
|
||||
|
||||
$url_params = parse_url($url);
|
||||
|
||||
if (!$url)
|
||||
{
|
||||
exit(0);
|
||||
}
|
||||
|
||||
$html = file_get_contents_curl($url);
|
||||
|
||||
$result = get_meta_from_html($html);
|
||||
|
||||
$result = array_merge(
|
||||
|
||||
get_meta_from_html($html),
|
||||
|
||||
array(
|
||||
'linkUrl' => $url,
|
||||
'linkText' => $url_params["host"] . $url_params["path"],
|
||||
)
|
||||
|
||||
);
|
||||
|
||||
echo json_encode($result);
|
||||
|
||||
?>
|
Loading…
Reference in a new issue