SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Help me with php curl and DOM???

    Help me with php curl and DOM???


    HTML Code:
    <form>
    <input type="text" name="linh" value="Hello word" />
    </form>
    --------------
    Trong file curl měnh viết:
    PHP Code:
    <html>
    <head>
    <meta charset="UTF-8" />
    </head>
    <body>

    <?php
    $handle 
    curl_init();
    curl_setopt($handleCURLOPT_URL"link");
    curl_setopt($handleCURLOPT_RETURNTRANSFERtrue);
    $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.
    Last edited by Mittineague; Feb 23, 2014 at 11:43. Reason: reformatting bbcode tags

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    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"'
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  3. #3
    SitePoint Zealot bronze trophy xMog's Avatar
    Join Date
    Mar 2011
    Posts
    145
    Mentioned
    3 Post(s)
    Tagged
    1 Thread(s)
    I'm not a 100% certain I understood your question, but here's a try:

    Code:
    // 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('');

  4. #4
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    If you only want the "linh" $_POST['linh'] value then it can be seen here in red:
    Code:
    
    <?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>
    Last edited by John_Betong; Feb 23, 2014 at 21:44. Reason: formatting
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all very much


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •