0

I am new at php, so bear with me.

I want to parse some html-data from a website, and i can do it with http://simplehtmldom.sourceforge.net/ pretty easy. The problem is that i need to get the data from a website, where i need to post some data, which means i can't just use the url which the library needs. I can use the action and post i a form to redirect to the website, but i can't get the information. Can you help me here?

Regards Jesper

gedemagt
  • 657
  • 1
  • 9
  • 22

1 Answers1

2

This code queries stackoverflow for your question using CURL:

$url = 'https://stackoverflow.com/search';

$params = preparePostFields( array(
     'q' => 'Getting data from a website where posted data is required',
   ));

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

curl_close($ch);

echo $result;

exit;

function preparePostFields($array) {
  $params = array();

  foreach ($array as $key => $value) {
    $params[] = $key . '=' . urlencode($value);
  }

  return implode('&', $params);
}

Regarding the CURLOPT_POSTFIELDS, please review this manual page:

Community
  • 1
  • 1
SteAp
  • 11,853
  • 10
  • 53
  • 88