Getting a single parse PHP value

Asked

Viewed 141 times

3

$ch = curl_init('site');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'x.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'x.txt');
$html = curl_exec($ch);
$dom = new DOMDocument();

@$dom->loadHTML($html);

$link = array();
foreach($dom->getElementsByTagName('a') as $link) {
  # Mostrar todos os elemento que estiver dentro da tag href


$x = $link->getAttribute('href'); // exibi todos os conteudo dentro do href !
// pegar apenas o link completo
#https://www.site.com/checkout.asp?ref=fvFDGND2MYQ

///
echo $x;

poem this way I can get all the html links received from Curl

http://prntscr.com/5icnbf

and for me to just take the https://www.site.com/checkout.asp?ref=fvFDGND2MYQ ? only that link ?

1 answer

1

If you want a link in particular, you can check the contents of the:

// ...
foreach ($dom->getElementsByTagName('a') as $link) {

  /* Verifica se é o link que eu quero
   */
  $href = $link->getAttribute('href');
  if (strpos($href, "checkout.asp?ref=") !== FALSE) {
    $x = $href;  // guarda o link
    break;       // sair do ciclo
  }
}

To verify, what was done was to check if the expression exists checkout.asp?ref= in the link to be processed using the strpos(). You should change the expression if you look for another link not what you have in question.

You can see an example working on Ideone combined with your previous problem.

  • 1

    :)) Now yes I caught only the link I need thank you very much ...

Browser other questions tagged

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