douqie1884 2015-05-21 15:13
浏览 230

CURL,如何获取变量隐藏输入值并同时发送POST请求(有一个请求)?

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.

  • 写回答

2条回答 默认 最新

  • duanbei3704 2015-05-21 15:30
    关注

    Once I worked on a similar scenario.

    This should be the strategy for you:

    1. First make a CURL request to the link that contains the form.

    2. Parse the page html and read the form hidden field (__RequestVerificationToken) value with regular expression match or any other library.

    3. Use the __RequestVerificationToken for your next request.

    4. As you mentioned the next request will return the form html, get the __RequestVerificationToken hidden field value again from the response html.

    5. 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.

    评论

报告相同问题?