简体   繁体   中英

serialize form to POST after getting content using CURL

I want to POST an URL using CURL and php.

There is a big form on webpage and I don't want to manually copy all the variables and put it in my POST request.

I am guessing there has to be a way to serialize the form automatically (using DOM or something) and then just change whatever values I need.

I could not google my way out of this one so I was wondering would anyone be kind enough to help.

So, is there anyway to automatically serialize a form which is buried in a bunch of html content I just pulled from a URL?

Thanks for any help, Andrew

$http = new HttpQueryString();
$http->set($_POST);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$http->get());

Requires PECL pecl_http >= 0.22.0

Its not to clear to me if you are asking how to get the form in the browser to the server or how to place the posted form in the curl request. From php, assuming the form posted over, it would be as simple as:

curl_setopt($handle, CURLOPT_POSTFIELDS, $_POST);

though no data is validated that way.

From the web side, not sure why you would serialize the form using the DOM/Javascript, as opposed to just submitting it via a normal post?

I don't understand the question. It sounds like you want to screen-scrape a form, fill it in, and then POST it back to the page you got it from. Is that right?

Edit in response to comment: I'd recommend scraping the CURL'd HTML with a tool like Simple HTML DOM (that's what I use for scraping with PHP). The documentation for your library of choice will help you figure out how to identify the form fields. After that, you'll want to curl the form's action page, with the CURL_POST_FIELDS attribute set to the values you want to pass to the form, urlencode() 'd of course.

Not sure what the question really is, but you're either wanting to do something like this:

$fields_string = http_build_query($data_to_send);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string);

or you need to look into this:

$html = file_get_html('http://www.thesite.com/thepage.html');
foreach($html->find('input') as $element) 
echo $element->name . '<br>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM