views:

734

answers:

4

Hi,

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

+1  A: 

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?

Mainegreen
A: 

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.

Jeremy DeGroot
exactly. I actually got the variables I need to post using firebug. (although i still can't do it automatically, which is what I was asking).
Andrew
A: 

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>';
Lance Kidwell
A: 
$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

lo_fye