I am trying to write an html file where I have a URL link and the like name is the title of the link itself.
In order to do so, I follow the answer proposed in the second answer of the following post: How can I get the title of a webpage given the url (an external url) using JQuery/JS
<!DOCTYPE html>
<html>
<head>
<script>
function getTitle(externalUrl){
var proxyurl = "./get_external_content.php?url=" + externalUrl;
$.ajax({
url: proxyurl,
async: true,
success: function(response) {
alert(response);
},
error: function(e) {
alert("error! " + e);
}
});
}
</script>
</head>
<body>
<!-- Link to news -->
<a href="https://www.corriere.it/economia/tasse/cards/irpef-2020-come-cambiano-scaglioni-studio-riforma-aliquote/mosse-governo_principale.shtml">Notizia Corriere</a>.
getTitle("https://www.corriere.it/economia/tasse/cards/irpef-2020-come-cambiano-scaglioni-studio-riforma-aliquote/mosse-governo_principale.shtml")
The PHP file instead is this:
<?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);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = $_REQUEST["url"];
$html = file_get_contents_curl($url);
preg_match('/<title>(.+)<\/title>/',$html,$matches);
$title = $matches[1];
echo json_encode(array("url" => $url, "title" => $title));
I have to say that I am not sure weather I am calling the .php function correctly and the same for the getTitle function. To many of you this may be a basic question but I have no html/php experience and I cannot get around this issue. Would you be able to lead me to the correct way to tackle this issue?