Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple HTML DOM Parser - Send post variables

Tags:

php

domparser

I have the Simple HTML DOM Parser for PHP, and I am using the following markup:

$html = file_get_html('http://www.google.com');

However how do I send post variables (like a cURL) to that page and get the response? For example

$html = file_get_html('http://www.google.com', array("Item"=>"Value", "Item2"=>"Value2"));
like image 963
rickyduck Avatar asked Feb 29 '12 10:02

rickyduck


1 Answers

The documentation doesn't mention it as far as I can see, but after taking a look in the source code I noticed the function you're using accepts a stream context as its third argument. You can create a post request with this PHP feature like this:

$request = array(
'http' => array(
    'method' => 'POST',
    'content' => http_build_query(array(
        'Item' => 'Value',
        'Item2' => 'Value2'
    )),
)
);

$context = stream_context_create($request);

$html = file_get_html('http://www.google.com', false, $context);

If you don't like contexts or would prefer a different method (like the cURL extension) you could also just fetch the page content using that, then feed it to the parser with str_get_html() or $parser->load(); the class itself does pretty much the same internally with the method you're using right now.

like image 111
Another Code Avatar answered Sep 27 '22 23:09

Another Code