Get a value that is within a span in another site using PHP

3

I would like to know how to get a value that is inside one of an external URL to use in a given calculation.

Example: link I want to get only the value for Soja s / Royalts, which is within this span: 56.50

    
asked by anonymous 21.07.2014 / 18:52

2 answers

2

In your case, the tag you want to get has no id or anything to identify it, so you can only get the value by its index:

<?php

// Desabilita erros da libxml e permite que o usuário obtenha informação do erro como necessitar 
libxml_use_internal_errors(TRUE);

$html = new DOMDocument();
$html->loadHTMLFile('http://www.agropan.coop.br/cotac.htm');

$spans = array();
foreach($html->getElementsByTagName('span') as $span)
{ 
    $spans[] = $span;
}

echo $spans[4]->nodeValue;

?>
    
21.07.2014 / 19:37
1

You can use a PHP Client that processes DOM of the document that is in this URI.

One component that can help you is the Goutte :

<?php

use Goutte\Client;

$client = new Client;
$crawler = $client->request('GET', 'http://www.agropan.coop.br/cotac.htm');
$crawler->filter('span')->each(function ($node) {
    print $node->text()."\n";
});
    
21.07.2014 / 19:19