Actually there’s nothing special about this, what you are looking isnt what i want to explain, i mean my main concern isnt to teach you how to make facebook like link extractor, my main concern was to learn PHP’s DOM extention. In short PHP DOM extension provides standard set of objects and interfaces to manipulate HTML as well as XML document. It represents HTML/XML page as objects. we can easily access HTML documents objects (tags) like “img”,”title” by writing few lines
$tags = $doc->getElementsByTagName(‘img’); will return set of all the images as an array.
You can read more about PHP DOM here
The DOMDocument class
DOM XML (PHP 4) Functions
Document Object Model
Talking about real facebook.com link extractor, its much faster than our Facebook like status update link extractore, its because every time you make request from Facebook like status update link extractor script it visits that site and extract the contents where facebook allready maintains the database of url, title, descriptions and images. so its much faster than this script. you you extents this script and use database to store the details or may be you can also use php memcache. This will make this script Facebook like status update link extractor much faster. I would have done it but i have to move to next script so i have again left this to you 😛
View Demo
[/sociallocker]
1 2 3 4 |
$doc = new DOMDocument(); @$doc->loadHTML($html); $tags = $doc->getElementsByTagName('img'); $title = $doc->getElementsByTagName("title"); |
You can read more about PHP DOM here
The DOMDocument class
DOM XML (PHP 4) Functions
Document Object Model
Talking about real facebook.com link extractor, its much faster than our Facebook like status update link extractore, its because every time you make request from Facebook like status update link extractor script it visits that site and extract the contents where facebook allready maintains the database of url, title, descriptions and images. so its much faster than this script. you you extents this script and use database to store the details or may be you can also use php memcache. This will make this script Facebook like status update link extractor much faster. I would have done it but i have to move to next script so i have again left this to you 😛
Final Product – Facebook like link extractor
Facebook like status update link extractor – Browser Support
IE | Tested in IE 6,7,8,9 |
Firefox | 10.0 (I believe it should work in all GECKO engine browsers like netscape) |
Chrome | 16 |
Safari | 5.1.2 |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
*{ font: 12px "lucida grande",tahoma,verdana,arial,sans-serif; margin: 0px; padding: 0px; } .container{ border: 1px solid #B4BBCD; width: 500px; min-height: 100px; position: relative; margin-left: 30%; margin-top: 5%; } .link-input { border-bottom: 1px dashed #B4BBCD; } .link-input textarea{ height: 60px; margin: 1px; width: 98%; vertical-align: top; margin-top: 5px; border: none; font: 12px "lucida grande",tahoma,verdana,arial,sans-serif; overflow: auto; padding-left: 5px; /* hack for crome */ outline:none; } .button-holder{ background: #F2F2F2; height: auto; height: 33px; font-weight: bold; } .button-holder #fb-button{ background: #5F78AB; border: 1px solid #29447E; color: #FFF; font-family: "lucida grande",tahoma,verdana,arial,sans-serif; font-size: 12px; font-weight: bold; margin-top: 5px; margin-right: 4px; float: right; padding: 2px 5px; box-shadow: 0 1px 1px #FFFFFF inset; cursor: pointer; } .button-holder #fb-button:hover{ background: #4A618E; } .extract{ display: none; padding: 10px; height:180px; } .float-left{ float: left; clear: right; } .extract-thumb { width: 30%; } .extract-info { width: 60%; margin-left: 5px; } .extract-info span{ width: 98%; display : block; text-align : left; margin : 5px 0px; } .extract-info #title{ font-weight: bold; } .extract-info #url{ color : #797979; font-size: 11px; } .nav{ text-align: left; width: 180px; } .nav img{ cursor: pointer; float:left; } .nav #navount{ color : #797979; font-size: 11px; width:60%; float:right; } .loading{ background:url("images/loading.gif") no-repeat; width: 16px; height: 11px; float: right; margin: 5px; margin-left: 95%; position: absolute; display: inline; width: 16px; visibility: hidden; } .delete{ background:url("images/delete.png") no-repeat scroll; display: inline-block; width: 15px; height: 15px; cursor : pointer; margin-left: 95%; } .delete:hover{ background:url("images/delete.png") no-repeat scroll 0px -30px; } |
Few lines of HTML
JavaScript (jQuery)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
$(document).ready(function(){ var imgArray; var title; var desc; var index = 0; $("#fb-button").click(function(){ var link = $("#link").val(); if(/^(http|https|ftp):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i.test($("#link").val())) { imgArray = new Array(); index = 0; title = ""; desc = ""; $(".loading").css("visibility","visible"); if(link.length){ // Encode url so we can have single line url instead of different parts elink = encodeURIComponent(link); $.ajax({ type: "POST", url: "link_details.php", data: "link="+elink, success: function(responce){ if(responce != "0") { var json = $.parseJSON(responce); $.each(json, function(key, val) { //alert(val.src); if(val.src != null){ imgArray.push(val.src); $("#trick").attr("src",val.src); //$(".array").append(" "+val.src); } //console.log(val); if(val.title != null) title = val.title; if(val.url != null) link = val.url; if(val.desc != null) desc = val.desc; }); //alert(title); if(imgArray.length > 0){ // if images found then show nav icons $(".nav").show(); // also hide image holder $(".extract-thumb").css("visibility","visible"); if($(".extract-thumb").html() == "") $(".extract-thumb").append('</pre> <img style="width: 100px;" src="'+imgArray[0]+'" alt="" /> <pre class="brush:javascript:">'); else $(".extract-thumb").html('</pre> <img style="width: 100px;" src="'+imgArray[0]+'" alt="" /> <pre class="brush:javascript:">'); }else{ // if images not found then hide nav icons $(".nav").hide(); // also hide image holder $(".extract-thumb").css("visibility","hidden"); } //console.log(title); $(".extract-info #title").html(title); $(".extract-info #url").html(link); $(".extract-info #desc").html(desc); showcount(index); $(".extract").slideDown("slow"); }else{ alert("This url doesnt exists !"); } $(".loading").css("visibility","hidden"); } }); }else{ alert("Please enter link"); } }else{ alert("Enter proper url, with http / https and www Ex: http://www.google.com"); } }); $("#next").click(function(){ if(index < imgArray.length){ index++; $(".extract-thumb").find("img").attr("src",imgArray[index]); showcount(index); } }); $("#prev").click(function(){ if(index > 0){ index--; $(".extract-thumb").find("img").attr("src",imgArray[index]); showcount(index); } }); $(".delete").click(function(){ $(".extract").fadeOut("slow"); }); showcount = function(index){ index = index + 1; if(index 0) $("#navount").html("Showing "+index+" of "+imgArray.length); }; }); |
Download code below
[sociallocker id=”1764″] Download[/sociallocker]
Facebook like status update link extractor
This is absolutely awesome Amit. How can you make it so it automatically extracts the pasted in link and also get rid of the actual url itself. That is exactly what Facebook does. Thanks and awesome job again.
Cool trick, I’ll try this.
hello amit . really u saved my many days . i have been searching for something which could be really well coded and easy to understand for data extraction . this is the best yet i came across . very well made . and also browsed this website . awesome work friend . m subscribing . hope 2 see many much articles for noobs like us . thanks 😉
Thanks john, for appreciating my work !!!
hy! I have a problem with your script on PHP 5.3.8. – SQL-Server version: 5.1.52
some links (http://www.rtl.de, http://www.cafe.ba, http://www.youtube.com) works fine and others (e. g. http://www.bild.de, http://www.taff.de, ….) don’t.
Error Message: “This url doesnt exists !”
This is because u forgot to add “http://” in the url i just tried “http://www.bild.de/” and it worked for me. it shows description and 112 thumb images. check once again with http://
[…] facebook like status update link extractor […]
[…] Facebook like status update link extractor […]
I have been searching for something which could be really well coded and easy to understand for data extraction. Use something like cURL to get the page and then something like Simple HTML DOM to parse it and extract the elements you want.
nice
Thanks very nice blog!
Heya i am for the first time here. I found
this blog and I find It truly useful & it helped
me out much. I hope to give something back and help others like
you aided me.
Thanks for script 😀
I am using the angular js in my site and i need link extractor using angular js
Is It possible using the angularjs ???
Yes its absolutely possible
Hi Amit,
I learn can you advise how save data base or i need loc img,title for send mysql thanks for you
Really nice Tutorial, glad to have found this site.
[…] Read More […]