3
I would like to know how to get a value that is within one of an external URL to use in a given calculation.
Example: http://www.agropan.coop.br/cotac.htm I want to take only the value related to Soy s/Royalts, which is within this span: 56,50
3
I would like to know how to get a value that is within one of an external URL to use in a given calculation.
Example: http://www.agropan.coop.br/cotac.htm I want to take only the value related to Soy s/Royalts, which is within this span: 56,50
2
In your case the tag you want to catch has no id or anything that identifies 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;
?>
Good solution, the DOM is quite verbose for a simple task, but it is very effective. However I suggest that you never use the arroba to suppress errors. Imagine that this HTML might not be loaded by the DOM. The arroba will force a FALSE and the Application that depends on this information will not know why it is not working and you will not have to debug because you have not received a I/O The Warning. Much better, in that case, is before you do the load of HTML, call libxml_use_internal_errors( TRUE );
This solution worked well. I will follow @Brunoaugusto’s guidance also not to suppress the error. Thank you!
perfect @Brunoaugusto, already edited the code as your suggestion
1
You can use a PHP Client that does the processing 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";
});
I won’t say anything about Goutte’s creator, but the drawback of this approach is the need for a second component, Guzzle.
@Brunoaugusto I’ve used Goute in some 3 projects. I haven’t used it for a couple of years, so I think there should be better components. If interaction is not required on the page, use of the DOMDocument
is OK
Just for the record that was not a criticism, in view of Fabio Potencier be a name of great weight in the community. : D
@Brunoaugusto Roger!
@Brunoaugust the Goutte’s own proposal is to be a wrapper between some components of Symfony and Guzzle, in order to facilitate the use: https://github.com/fabpot/goutte#Technical-information. If you don’t want to use it, just implement something on top of Guzzle.
But I didn’t say that :(
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
Maybe it’ll work with a library to make parse of XML...
– user7261
You who developed the code from where you want to take the values? If it was you, and the two files are in the same project, you could save the value you need in the section and retrieve it further.
– Dante
I didn’t develop it myself, but the @user2479421 solution worked well. Thank you!
– mealine