-2

With this code I print

<?php
    $curlSession = curl_init();
    curl_setopt($curlSession, CURLOPT_URL, 'https://www.google.com');
    curl_setopt($curlSession,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
    $homepage = curl_exec($curlSession);
    curl_close($curlSession);
    echo $homepage ;
?>

How can I get and print just the search form?

Thanks

palaѕн
  • 72,112
  • 17
  • 116
  • 136
Lilia
  • 139
  • 4
  • 1
    See: https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – John Conde May 16 '20 at 13:46
  • I've read your link but I do not undertood what code I can use for my purpose. – Lilia May 16 '20 at 14:13
  • Does this answer your question? [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – hanshenrik May 16 '20 at 15:18

1 Answers1

1

parse out the form with DOMDocument, eg

$domd=@DOMDocument::loadHTML($homepage);
$searchForm=$domd->getElementsByTagName("form")->item(0);
$searchFormHTML=$domd->saveHTML($searchForm);
echo $searchFormHTML; 
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • @Lilia yeah but you need the xml extension for php, eg if you're compiling php yourself you need to add `./configure --with-libxml` before compiling it, or if you're using Ubuntu/Debian you need to run `sudo apt install php-xml` – hanshenrik May 16 '20 at 19:10
  • and what if the remote page has more than 1 form tag? – Lilia May 16 '20 at 19:17
  • @Lilia foreach() - ```foreach($domd->getElementsByTagName("form") as $form){var_dump($domd->saveHTML($form));}``` will dump the html for all forms in the html - maybe you should google ```php DOMDocument tutorial``` – hanshenrik May 16 '20 at 19:19