Jump to content

How to get Form Field with php?


Lukeidiot

Recommended Posts

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>

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);	
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.