Solved

PHP get values from hidden input fields

Posted on 2014-02-10
8
1,316 Views
Last Modified: 2014-02-10
Hi Expert,

I like to retreive a variating number of values from An external page and store them in a array.

I belive I'm allmost there but I can't get the array to print its content.
and I only need the value's to be loaded on the array without the HTML.

The source page looks like this:

<html>
<head>
<title>test</title>
</head>
<body>
<input type="hidden" name="token1" value="5UXwe6sMWf5JZQ79B32LeY2">
<input type="hidden" name="token2" value="10sx11SdUKnaDCpd1C6T88i">
<input type="hidden" name="token3" value="92ZbU111xhU4cehCXL9b1Oy">
<input type="hidden" name="token4" value="gjEBf8vo4akmvqG5e6g7jwH">
<input type="hidden" name="token5" value="B8H3jEsE7svP3EC5Rh0nt0s">
<input type="hidden" name="token6" value="JH2nDrp69WQkh11woL695hJ">
</body>
</html>

Open in new window


The data retreiving site :

<?
$url = "http://www.mydomain.nl/tokens.php?num=6";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);

$dom = new DOMDocument;
$dom->loadHTML( $output);

    $hidden_inputs = array();

    // 1. get all inputs
    $nodes = $dom->getElementsByTagName('input');

    // 2. loop through elements
    foreach($nodes as $node) {
        if($node->hasAttributes()) {
            foreach($node->attributes as $attribute) {
                if($attribute->nodeName == 'type' && $attribute->nodeValue == 'hidden') {
                    $hidden_inputs[] = $node;
                }
            }
        }
    } unset($node);


    foreach($hidden_inputs as $node) {
        echo "<pre>" . htmlspecialchars($dom->saveHTML($node)) . "</pre>";
    } unset($node);

?>

Open in new window


Now I get this error:
 "Catchable fatal error: Object of class DOMElement could not be converted to string in retreive.php on line 29"

line 29 is the last echo line in the loop
0
Comment
Question by:Steynsk
  • 5
  • 3
8 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39847091
How sure are you that the test data (first code snippet) is actually representative of the genuine article?
0
 
LVL 1

Author Comment

by:Steynsk
ID: 39847101
Hi Ray,

I don't understand your question/point?

I did discover the error was wrong.

It should be :
Warning: DOMDocument::saveHTML() expects exactly 0 parameters, 1 given in line 29
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39847112
I'll be glad to try to show you the code; just want to be sure that I'm using accurate test data.  The sample looks pretty hypothetical.  Can you please post a link to the actual URL you want to read with cURL?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 1

Author Comment

by:Steynsk
ID: 39847117
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39847329
Great, thanks.  I'll see what I can do with it and post back with the results.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39847376
This seems to do the trick.  Probably a little more efficient than loading the entire DOM.
http://www.laprbass.com/RAY_temp_steynsk.php

<?php // RAY_temp_steynsk.php
error_reporting(E_ALL);
echo '<pre>';


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28360742.html


//  READ TEST DATA
$url = 'http://www.idiezijn.nl/research/codes.php?usercode=y3J4dpgBPgeldL01odaE6q8&userpass=kn8l4DEuO5uKIpO8Usb5Jfs&num=6&hospital=1';
$str = file_get_contents($url);

// ACTIVATE THIS TO SHOW WHAT WE READ
// echo htmlentities($str);

// A REGULAR EXPRESSION TO MATCH THE VALUE ATTRIBUTES
$rgx
= '#'        // REGEX DELIMITER
. 'value="'  // SIGNAL STRING
. '('        // CAPTURE GROUP
. '.*?'      // ANYTHING
. ')'        // END CAPTURE GROUP
. '"'        // THE ENDING QUOTE
. '#'        // REGEX DELIMITER
. 'i'
;

// PERFORM THE MATCH
preg_match_all($rgx, $str, $mat);

// THE VALUES ARE STORED HERE
print_r($mat[1]);

Open in new window

Best regards, ~Ray
0
 
LVL 1

Author Closing Comment

by:Steynsk
ID: 39847428
Thanks Ray you helped me out big time.

Best regards,

Steynsk
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39847630
Great!  Thanks for using EE and best of luck with your project, ~Ray
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hosting images 4 27
How to populate a dynamically created text input from ajax return 18 45
Presenting past 6 months bug 7 26
How is this connection happening? 3 11
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question