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
5
  • I'm not familiar with PHP, but this didn't look right so a quick search yielded this. $dom->getElementById('hosted_button_id'); Commented Dec 6, 2013 at 16:57
  • Parsing the HTML string fails. $dom is FALSE. Try closing the input and img tags like <input ... /> Commented Dec 6, 2013 at 16:59
  • @WebDevNewbie Its not an ID, its a name Commented Dec 6, 2013 at 16:59
  • @MarcellFülöp Its Paypal generated html, would rather not go through a whole str_replace on it every time Commented Dec 6, 2013 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. Commented Dec 6, 2013 at 17:01

4 Answers 4

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');
Sign up to request clarification or add additional context in comments.

2 Comments

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
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).
1

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

1 Comment

oh ok ok i have less exp of paypal sorry about that
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); 

1 Comment

This will not work. getElementsByTagName(). You can use $dom->getElementsByTagName("input") to get input tags.
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");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.