I am trying to submit a form via PHP CURL library. The problem is there is a hidden input field in which its value changes after each refresh, it means that it changes when I send to different requests to read the page content. I need that hidden value to be sent among the other fields in my POST request for the form submission to be submitted successfully.Since the action of the form that I want to submit is "itself" (the same page) I do not know how to fetch the hidden input value, put it in the POST request and send the POST request which would be considered roughly simultaneous. I have used a specific library for my CURL so even if I share the code you will not completely understand it.Anyway, I think I have clearly explained what I am confused about. Looking forward to hearing from you.
P.S. You might say, it will not be simultaneous, one request to fetch the form and one another to send the POST request. Indeed, the problem is when I fetch the page content to get the hidden value(TOKEN) it has value "X" and when I want to send the POST request after it, it will have value "Y". I want to have "X" and utilize it in submitting the form with "X" going to take value "Y". That is the simplest way to explain what I want.
Once I worked on a similar scenario.
This should be the strategy for you:
First make a CURL request to the link that contains the form.
Parse the page html and read the form hidden field (__RequestVerificationToken) value with regular expression match or any other library.
Use the __RequestVerificationToken for your next request.
As you mentioned the next request will return the form html, get the __RequestVerificationToken hidden field value again from the response html.
- Use it for subsequent request and repeat the same process.
As you are not telling more details. What I assume is you are working with PHP
to submit data to .NET
based website form. If this is the case, just capturing the __RequestVerificationToken won't be enough. You have to take care of other sensitive headers
like Cookies
, User-Agent
, any kind of custom headers etc.
The "grep-to-variable" bit works differently on a Mac OS, because there is no "grep -P". (See grep -P no longer works how can I rewrite my searches for details.)
Given five instances of something like this in an HTML page:
<input name="_dynSessConf" value="-5345230103218767463" type="hidden"></input>
...I used a curl command to pull down my page into "output.txt" and then pulled the value of _dynSessConf into a an environment variable using a RegEx via perl.
dynSessConf=$(perl -nle 'print $& if m{.*_dynSessConf" value="\K[^"]+}' output.txt | tail -1)
I know this isn't the "one-liner" you were looking for, but it could probably be tweaked to give you a one-liner on a Mac.