0

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.

Ayyash
  • 4,257
  • 9
  • 39
  • 58

3 Answers3

1

There is an answer here on how to do it in PHP. If you are wanting to use Ajax, you can use JSONP with PHP. The instructions for exactly how to do this are here

bsimic
  • 926
  • 7
  • 15
  • jsonp wont work because im trying to read meta tags of an html page, and i wish I knew if that PHP method works from localhost or does it bring back the same "same-origin-policy" problem – Ayyash Mar 14 '12 at 05:52
  • ive finally went down the ugly road and found out that localhost does not affect the php method, and the way to do it using ASP.NET is through WebClient or StreamReader functions... but its almost impossible to know which parts to read unless i go through a hefty lot of parsing :( – Ayyash Mar 15 '12 at 07:19
1

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);
Ayyash
  • 4,257
  • 9
  • 39
  • 58
0

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...)

Community
  • 1
  • 1
Cheruiyot Felix
  • 1,557
  • 5
  • 26
  • 42
  • thanks, but im very sure i tried C# webclient methods and I got the same "same-origin-policy" problem... is this nice php method replicated in c#? – Ayyash Mar 13 '12 at 15:14
  • It might be. I am researching on it i'll get back to you. – Cheruiyot Felix Mar 13 '12 at 15:24
  • look at what I found http://stackoverflow.com/questions/3536800/same-origin-policy-ajax-using-public-apis – Cheruiyot Felix Mar 13 '12 at 15:25
  • thats more specific to js content, im trying to fetch an html, i wish there was a way to inject an html page – Ayyash Mar 13 '12 at 15:34
  • let me research on it.Ill get back to you. – Cheruiyot Felix Mar 13 '12 at 15:45
  • I think bsimic solution above solves our case.I tried the code and it works. Let me know if that is what you were looking for. We just have to handle the obtained data well and save it to the database. Using that data you can create the application such as that of google and facebook. I think this is the trick used: Fetching pages,stored to db,use ajax to retrieve the information or simply use json to render the results directly – Cheruiyot Felix Mar 13 '12 at 19:22