Help me with php curl and DOM?

Help me with php curl and DOM???

<form>
<input type="text" name="linh" value="Hello word" />
</form>

Trong file curl mình viết:

<html>
<head>
<meta charset="UTF-8" />
</head>
<body>

<?php
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, "link");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($handle);
curl_close($handle);

$DOM = new DOMDocument;
@$DOM->loadHTML($html);


// Help me get value from iput name = "linh"

//var_dump($items);


?>
</body>
</html>

Thanks all.

I have recently done this by passing value=‘Hello World’ as GET parameter to curl(). I am using a tablet at the moment and when back on my computer will paste the source.

Try replacing ‘link’ with ‘http://link.php?name=“Hello World”’

I’m not a 100% certain I understood your question, but here’s a try:


// Get all <input ...> fields
$inputs = $dom->getElementsByTagName('input');

// value found for input
$val="";

// for each input nodes
foreach ($inputs as $input) {
  // if the input nodes has an attribute named "linh"
  if ($input->getAttribute('name') == 'linh') {
     //get the value of the attribute "value" into the variable $val
     $val = $input->getAttribute('value');
     break;
  }
}

echo "value found=" . $val;

I didn’t test the code but it should be near the solution. If you can change the HTML that your read, you could add an “id” to the input element, it would make this easier because you could use $dom->getElementById(‘’);

If you only want the “linh” $_POST[‘linh’] value then it can be seen here in red:




<?php $title='file: curl-test.php';?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Language" content="en"/>
  <title><?=$title;?></title>
  <style type='text/css'>
    pre,
    div {width:88%; margin:2em auto; background-color: #cff; color: #000; padding: 1em;}
    h2  {background-color: #ff9; color: #f00; font-weight:700;}
  </style>
</head>
<body>
  <h1><?=$title;?></h1>
  <form action="?" method="post" class="tac">
    <div>
      <input type="text" name="linh" value="Hello word" />
    </div>  
  </form>
  <hr /><hr />


<?php
  if(isset($_POST)):
    echo '<h4>Curl Stuff here</h4>';
    $handle = curl_init();
      $link   = 'http://localhost/_album/test.php';
        curl_setopt($handle, CURLOPT_URL, "$link");
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
      $html = curl_exec($handle);
    curl_close($handle);


    echo '<pre> print_r($html) === ';
      var_dump($html);
    echo '</pre>';
    echo '<h4>Test.php File Contents:</h4>';
    echo '<div style="background-color: #ddd; color: #090">';
      highlight_file('test.php');
    echo '</div>';    


    $DOM  = new DOMDocument;
    $html = @$DOM->loadHTML($html);
    echo '<pre> var_dump($html) === ';
      var_dump($html);
    echo '</pre>';


  else:
    echo __FILE__; 
  endif;
  echo '<hr /><hr />';
  echo '<h4>Help me get value from input name = "linh"</h4>';
  echo '<h2>$_POST["linh"] === ' .$_POST["linh"] .'</h2>';


  echo '<pre> print_r($_POST) === ';
    $items = $_POST;
    var_dump($items);
  echo '</pre>';
  echo '<hr /><hr />';


  echo '<pre> Source code: ';
    highlight_file(__FILE__);
  echo '</pre>';
  echo '<hr /><hr />';
?>
</body>
</html>


Thanks all very much