Take value from a Span field with Curl Parser

Asked

Viewed 485 times

1

I have a question and a problem. Good is the following with Curl I login beauty so far everything well.. but I need to get some information inside the html:

<span id="Number">12345678993</span>
<span id="holderName">RAFAELA VERCOSA MARIANO</span>
<span id="expirationDate">11/2019</span>

The function I’m using is:

function pegar_oneclick(){
   $nome = '';
   $html = str_get_html($this->http_response);
   foreach($html->getElementById('holderName') as $element){
      $text   = $element->plaintext;
      $text   = str_replace(array("\r", "\n", "\t"), ' ', $text);
      $nome .= str_replace('  ', '', $text);
   }

   return $nome;
}

I want you to return only the Number: value, holderName: RAFAELA VERCOSA MARIANO and expirationDate, I don’t want anything ready just an orientation of how I should do it.

1 answer

1

You must use getElementsByTagName instead of getElementById.

See an example using DOM:

$DOM = new DOMDocument;
$DOM->loadHTML($html);
$items = $DOM->getElementsByTagName('span');

// Imprimindo os valores
for ($i = 0; $i < $items->length; $i++)
    echo $items->item($i)->nodeValue . PHP_EOL;

// Saída:
// 12345678993
// RAFAELA VERCOSA MARIANO
// 11/2019

DEMO

Your code didn’t work because through foreach you were only looking for id holderName. Your code should look like this:

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

$number = $DOM->getElementById('Number')->nodeValue;
$holdername = $DOM->getElementById('holderName')->nodeValue;
$expirationdate = $DOM->getElementById('expirationDate')->nodeValue;

echo $number. PHP_EOL;         // 12345678993
echo $holdername. PHP_EOL;     // RAFAELA VERCOSA MARIANO
echo $expirationdate. PHP_EOL; // 11/2019

DEMO

  • Good evening friend, I followed your guidance and the same could not see http://pastebin.com/jn0swJXz

  • @Brunohenrique The url has to be https and not http. The code doesn’t work because there is no field Number, holderName or expirationDate. Look at the source code of the page and see!

  • See here, https://privnote.com/n/ohudrgrunbyqeuif/#rrrvsdyaubreyqok

Browser other questions tagged

You are not signed in. Login or sign up in order to post.