fetch remote page title via ajax vs php
-
13-12-2019 - |
Question
Please help, clarify this issue, I have seen it on many sites now, you type the url, it brings back the title and description and an image as well (facebook for example), how do they do that? tried by ajax on localhost im getting same-origin policy headache.
If not ajax, can u use web streams on server side (php or .net)?
I've looked through stackoverflow and cant find answers. Thank you.
Solution 2
To read from another web client-side Ajax does not work, to do that in C# we use
System.Net.WebClient wc = new System.Net.WebClient();
byte[] response = wc.DownloadData(fileName);
sContents = System.Text.Encoding.UTF8.GetString(response);
Reading content means parsing the text and looking for certain tags using Regular Expressions, I found no library that'd do the horrendous activity so I had to do it myself
for title
Match TitleMatch = Regex.Match(strIn, "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
for description
Match DescriptionMatch = Regex.Match(strIn, "<meta name=\"description\" content=\"([^<]*)\">", RegexOptions.IgnoreCase | RegexOptions.Multiline);
OTHER TIPS
Try to consider this:
file_get_contents('http://somesite.com.au');
get_meta_tags('http://somesite.com.au');
more info Get information from a web page (title, pictures, heads, etc...)