1

I'm trying to POST a value to a page using a html form and get that page's source.

I can get the source using $html = file_get_html('http://www.exam.com/results/');

but before getting the souce, I need to POST the value to that page first, and then grab the source.

lets say other page which I should post the value is http://www.exam.com/results/

and I created a form for value submitting.

<form method="post" action="http://www.exam.com/results/">

<input type="hidden" value="900358967" name="eid">
<input name="confirm" type="submit" value="Enter" >

</form>

so it will POST the value and show data on that page, but how to grab the page source with the data?

Is this possible? I searched everywhere and tried to grab it but I don't know how to grab after POST-ing the value.

naveencgr8
  • 331
  • 1
  • 4
  • 13
  • With Page Source what do u mean? Can you explain more? – chandresh_cool Apr 10 '13 at 09:59
  • I think you will need to use cURL for this purpose. Use cURL to post values to the form and return the resulting page source might work. http://php.net/manual/en/book.curl.php – viclim Apr 10 '13 at 09:59
  • If I understood the question you would like to get the source code of the page the POST was send from? – Brainfeeder Apr 10 '13 at 10:00
  • @chandresh_cool : its like this, I'll post a exam id to my school's web site, so it displays the exam results on that page, if I could grab the the page source with the results, I can filter the results section and display the results on my web site. – naveencgr8 Apr 10 '13 at 10:07
  • 1
    I think you are creating unnecessary chaos using the words "page's source", when you are talking about PHP (which has it's sources too, obviously, which you cannot get after you POST something to it), when in fact what you want is the resulting HTML (source). – Smuuf Apr 10 '13 at 10:35

2 Answers2

1

I think you have to try the same with CURL

//set POST variables
$url = 'http://www.exam.com/results/';
$fields = array(
'eid' => urlencode('900358967')
);
$fields_string = "";

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • Hi, it gives an error - Notice: Undefined variable: fields_string in C:\wamp\www\results\1\1.php – naveencgr8 Apr 10 '13 at 10:16
  • @naveencgr8 Forgot to initialise it now check..:) – Yogesh Suthar Apr 10 '13 at 10:17
  • Seems like it works, but it doesn't show the results, it shows the normal text box to enter the exam id.. means the value doesn't POST as we expected.. – naveencgr8 Apr 10 '13 at 10:27
  • @naveencgr8 use `print_r($result)` to check what it returns. – Yogesh Suthar Apr 10 '13 at 10:29
  • ok, and I have made a mistake when I'm typing the html form in the question, I edited the code of the html form. any thing should be changed because of that? – naveencgr8 Apr 10 '13 at 10:31
  • @naveencgr8 no nothing you have to change my code. – Yogesh Suthar Apr 10 '13 at 10:33
  • no still the same, it shows the text box to enter the id... – naveencgr8 Apr 10 '13 at 10:36
  • 1
    @naveencgr8 The schools website might be protected against 'fake' POST actions... Not sure if what you are trying to do is legal... Unless the school asked you to hack into their website to get the exam score ... – Brainfeeder Apr 10 '13 at 10:50
  • @Brainfeeder : Yeah, I know but its really not hacking though, so its not a big deal.. :) but your point is right.. – naveencgr8 Apr 10 '13 at 12:38
  • @YogeshSuthar: Hi, I think I might have done this wrong but your code was working. I found that i need to POST examid and year both together, now its working fine.. thanks for your time. – naveencgr8 Apr 10 '13 at 12:40
1

I noticed you have the question tagged with javascript. So Here is a solution that could work for you using jQuery.

$.post('http://www.exam.com/results/',$('form').serialize(), function(data) {
    alert(data);
});

If the URL only returns html, the html will be in the data var returned by the function.

Brainfeeder
  • 2,604
  • 2
  • 19
  • 37