Lukeidiot Posted November 14, 2010 Share Posted November 14, 2010 I am trying to find this form field "form_key" on tumblr.com If you view source on: http://www.tumblr.com/dashboard On firefox it shows this: <form style="width:0px; height:0px; overflow:hidden; position:absolute;" method="post" action="/set_avatar" id="set_avatar_form" enctype="multipart/form-data"> <input type="hidden" name="form_key" value="jg351pzpvzMSkyJIZZhkT7AfcQ"/> <input type="file" name="set_avatar" id="set_avatar" onchange="$('set_avatar_form').submit();"/> <input type="hidden" name="redirect_to" value="/dashboard"/> </form> But when I use php's file_get_content("http://www.tumblr.com/dashboard"); That data is no where to be found. Any idea how I can get that form data in my php file? For example I am trying to use regex preg_match_all to extract the form_key, but get_file_contents doesnt show the <form></form> Quote Link to comment Share on other sites More sharing options...
objnoob Posted November 14, 2010 Share Posted November 14, 2010 get_file_contents will not retreive the html of a URL. You should use the cURL extension and set option return transfer to true. Quote Link to comment Share on other sites More sharing options...
Lukeidiot Posted November 14, 2010 Author Share Posted November 14, 2010 get_file_contents will not retreive the html of a URL. You should use the cURL extension and set option return transfer to true. I am using cURL, but can you show me an example on how to use it to get the html? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 14, 2010 Share Posted November 14, 2010 get_file_contents will not retreive the html of a URL. You should use the cURL extension and set option return transfer to true. file_get_contents() will get the HTML of many URLs. Quote Link to comment Share on other sites More sharing options...
Lukeidiot Posted November 14, 2010 Author Share Posted November 14, 2010 get_file_contents will not retreive the html of a URL. You should use the cURL extension and set option return transfer to true. file_get_contents() will get the HTML of many URLs. get_file_contents will not retreive the html of a URL. You should use the cURL extension and set option return transfer to true. file_get_contents() will get the HTML of many URLs. Not sure if that was a mispell, but "file_get_content()" is not a php function. It also doesnt get <form> html for some reason. I just dont know how to use cURL's returntransfer to read html. If anyone can tell me how to use cURL to get html, I would love you. Thanks! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 14, 2010 Share Posted November 14, 2010 what you get from a URL depends on how the server sends it. this code gets form html for me, from this particular site: $content = file_get_contents("http://www.headshopfinder.com/head-shop-finder.html"); echo "content: $content"; what i'm sort of driving at is the server might check to see if you are a real web browser or not and serve content dependent on that. if you perform a file_get_contents() and get nothing, it's likely that either a client-side script is in play and/or the server won't send because it knows you aren't a browser. Quote Link to comment Share on other sites More sharing options...
objnoob Posted November 14, 2010 Share Posted November 14, 2010 Ok, I'll shut up now! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 14, 2010 Share Posted November 14, 2010 here is part of the code i have used to download full HTML from websites: $out = $fopen("/location/to/output/file.txt",'w'); $url = "http://www.somewebsite.com/somepage.html"; $source = fopen($url,"r"); while ($a = fread($source,1024)) { $page .= $a; } fwrite($out, $page); fclose($out); fclose($source); Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted November 14, 2010 Share Posted November 14, 2010 on the other hand, objNoob is completely correct that you'll want to user curl functions if you need to spoof a browser in any way: cookies, referrer, user-agent, etc., etc. Quote Link to comment Share on other sites More sharing options...
Lukeidiot Posted November 14, 2010 Author Share Posted November 14, 2010 Yeah the file_get_contents doesnt work for this server. Does anyone know how I can use cURL to get the HTML Source? Quote Link to comment Share on other sites More sharing options...
objnoob Posted November 15, 2010 Share Posted November 15, 2010 Yes! $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $html = curl_exec($ch); Quote Link to comment Share on other sites More sharing options...
Lukeidiot Posted November 15, 2010 Author Share Posted November 15, 2010 Yes! $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $html = curl_exec($ch); That just loads the site though, I dont see any HTML Quote Link to comment Share on other sites More sharing options...
objnoob Posted November 15, 2010 Share Posted November 15, 2010 Don't echo $html. Use RegEx or DOMDocument Class to extract form_key value from $html. Quote Link to comment Share on other sites More sharing options...
Lukeidiot Posted November 15, 2010 Author Share Posted November 15, 2010 Don't echo $html. Use RegEx or DOMDocument Class to extract form_key value from $html. So like this? <?php $curl = curl_init(); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7"); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_COOKIEFILE, "c:/xampp/htdocs/tumcookie.txt"); curl_setopt($curl, CURLOPT_COOKIEJAR, "c:/xampp/htdocs/tumcookie.txt"); curl_setopt($curl, CURLOPT_URL, "www.tumblr.com/dashboard"); $login = curl_exec ($curl); preg_match_all('~name="form_key" value="([^"]+)"~', $login, $matches); echo $matches[0]; curl_close ($curl); ?> Quote Link to comment Share on other sites More sharing options...
objnoob Posted November 15, 2010 Share Posted November 15, 2010 If that works for you Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.