0

I have the following Paypal button code:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="ABCDEFG">
<input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>

I need to extract the value from the input with name hosted_button_id

This is what I'm trying:

$html = '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="ABCDEFG">
<input type="image" src="https://www.paypalobjects.com/en_US/GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal – The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>';

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

$pp_code = $dom->getAttribute('hosted_button_id');

echo "code is $pp_code"; die;

But i get the errors:

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: error parsing attribute name in Entity, line: 1 in /home/parkview/DB8NP8XA/htdocs/ajax/actions/addEvent.php on line 20

Fatal error: Call to undefined method DOMDocument::getAttribute() in /home/parkview/DB8NP8XA/htdocs/ajax/actions/addEvent.php on line 22
Toni Toni Chopper
  • 1,833
  • 2
  • 20
  • 29
StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • I'm not familiar with PHP, but this didn't look right so a quick search yielded this. $dom->getElementById('hosted_button_id'); – WebDevNewbie Dec 06 '13 at 16:57
  • Parsing the HTML string fails. `$dom` is FALSE. Try closing the input and img tags like `` – marekful Dec 06 '13 at 16:59
  • @WebDevNewbie Its not an ID, its a name – StudioTime Dec 06 '13 at 16:59
  • @MarcellFülöp Its Paypal generated html, would rather not go through a whole str_replace on it every time – StudioTime Dec 06 '13 at 17:01
  • @DarrentSweeney Good point, didn't notice. According to the DomDocument documentation where I got the code from, there is getElementsByTagName() with which you could probably index the first result to get your element. – WebDevNewbie Dec 06 '13 at 17:01

4 Answers4

1

Explanation about the "Warning" you get:

While malformed HTML should load successfully, this function may generate E_WARNING errors when it encounters bad markup. libxml's error handling functions may be used to handle these errors.

When your HTML markup is bad - you get a warning which you can disable by following this solution: https://stackoverflow.com/a/6090728/998096

Explanation about the "Fatal Error" you get:

There's not such a method "getAttribute" for DOMDocument, you need first to get the Element. The Element has that type of method. For instance:

 $xml = new DOMDocument(); 

// Load the url's contents into the DOM 
$xml->loadHTMLFile($url); 

//Loop through each <a> tag in the dom and add it to the link array 
$link = $xml->getElementsByTagName('a');
$url = $link->getAttribute('href');

Taken from PHP Manual (Comment).

Consider adding an "id" attribute to that specific input field, and use:

$html->getElementById('the_input_id');
Community
  • 1
  • 1
Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39
  • I know I can do it that way just didn't want to loop through all inputs if didn't have to as name never changes - seems I have no choice - can't add ID as it's Paypal code - thanks for the reply – StudioTime Dec 06 '13 at 17:15
  • If there's only 4 inputs a loop wouldn't be the worst. You can also try using `regex` (while i'm not excited about using it in those kind of situations). – Ofir Baruch Dec 06 '13 at 17:19
1

add id attribute in that field and you can just use getElementById("hosted_button_id").value for the value of that id

Daman Mokha
  • 372
  • 1
  • 3
  • 16
0

Adding my comment as an answer..

http://www.php.net/manual/en/domdocument.getelementsbytagname.php

As mentioned in my comment, my PHP is lacking, so hopefully this will be of use. This should grab all elements containing "hosted_button_id" as the name. Assuming you only have one, you should be able to grab the first and only element at index 0 in that NodeList.

$dom->getElementsByTagName("hosted_button_id")->item(0); 
WebDevNewbie
  • 1,833
  • 1
  • 13
  • 17
0

This is what I'm doing as can't extract single element value:

$inputs = $dom->getElementsByTagName("input");

foreach ($inputs as $input) {
    if ($input->getAttribute("name") == "hosted_button_id") {
        $pp_code = $input->getAttribute("value");
    }
}
StudioTime
  • 22,603
  • 38
  • 120
  • 207