开发者

How to obtain a value of <input> in remote HTML codes using PHP?

In a remote site, there is a HTML file (say http://www.example.com/abc.html), which reads:

<input id="ID1" name="NAME1" value="VAL开发者_StackOverflowUE1">

In my PHP code in my server, I need "VALUE1" from http://www.example.com/abc.html. How can I do it using PHP?

Since the remote html is written in XHTML 1.0, I guess I could use an XML parser?


ADDED

Using xml_parse_into_struct, I obtained an array that contains:

[15] => Array
    (
        [tag] => INPUT
        [type] => complete
        [level] => 4
        [attributes] => Array
            (
                [TYPE] => hidden
                [NAME] => NAME1
                [ID] => ID1
                [VALUE] => VALUE1
            )

    )

How can I obtain "VALUE1"? I guess now this is more a question for handling arrays in PHP. I always know the name "NAME1", but I don't know the value "VALUE1". So I want to obtain "VALUE1" using "NAME1" which is the information I know.


Why not just using a simple regex?

$html = '<input id="ID1" name="NAME1" value="VALUE1">';

if (preg_match('/name="NAME1".+value="(.*?)"/i', $html, $matches)) {
   echo $matches[1];  // should echo VALUE1;
}

The only constraint is that name must appear before value in the HTML element.


Its all going to depend on how you will be fetching your entire array. But taking the example above you can get the value by $array[15]['attributes']['VALUE'] Where the variable $array is the variable used to render your xml_parse_into_struct output to. But if you want it dynamic I suggest something a little more smarter as I think the key index 15 will change if more elements are added to the page.

$array = xml_parse_into_struct($string);
foreach ($array as $key => $value) {
  if($value['attributes']['NAME'] == 'NAME1') {
    $input_value = $value['attributes']['VALUE'];
    break; // unless you need to do more here just break out.
  }
}

print $input_value;


If you know the name of the element and are truly only after one little thing and the format of the page is always the same, it might be less work to just use curl and explode to parse the document with string compares. This is a quick-and-dirty way to do it, but as long as those two conditions are met this is arguably the fastest way:

$url = 'http://example.com/';
$options = array(
CURLOPT_RETURNTRANSFER => true,     // return web page
CURLOPT_HEADER         => false,    // don't return headers
CURLOPT_FOLLOWLOCATION => true,     // follow redirects
CURLOPT_ENCODING       => "",       // handle all encodings
CURLOPT_USERAGENT      => "spider", // who am i
CURLOPT_AUTOREFERER    => true,     // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
CURLOPT_TIMEOUT        => 120,      // timeout on response
CURLOPT_MAXREDIRS      => 10       // stop after 10 redirects
);

$ch      = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err     = curl_errno( $ch );
$errmsg  = curl_error( $ch );
$header  = curl_getinfo( $ch );
curl_close( $ch );
$parts = explode('<input id="ID1" name="NAME1" value="', $content);
if (count($parts) == 2) {
    $value = explode('">', $parts[1]);
    $value = $value[0];
} else {
    $value = false;
}

print 'Value is: ' . $value;

Otherwise, you could use regex (again using curl as above):

preg_match('/name="NAME1".+value="(.*?)"/i', $html, $value);
$value = $value[1];

Finally, if you want to go all-out on this one, you can use a document parser. Be warned, however, that if the HTML you are working with is not properly formed, the parser will have trouble. Here's a tutorial on the subject, using a third-party class: http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/


If you need to pass variable from html page to PHP code, use forms ( http://www.w3.org/TR/html4/interact/forms.html ) in html and $_POST ( http://www.php.net/manual/en/reserved.variables.post.php ) or $_GET ( http://www.php.net/manual/en/reserved.variables.get.php ) variables in PHP. If you are not familiar to arrays in PHP, take a look at this: http://www.php.net/manual/en/language.types.array.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜