file_get_contents

Asked

Viewed 215 times

-2

  • You can use this api, http://api.fixer.io/latest?base=USD the base is usd, then one of the values is Brl

  • Your question became a bit wide. Post what you tried, and the exact difficulty you are having, so that it is possible to help. As I say that to want to extract data directly from page in these cases is usually something extremely precarious, you would probably invest more time looking for some API that provides the data you search for. Pages are made for humans to obtain information, Apis are made for machines and systems to obtain information.

1 answer

1


I made a code here and got the result:

$html = file_get_contents( "https://www.google.com/finance?q=USDBRL&ei=tb1KWOHWLMX_mAGJyLnIBw" );
$content = new DOMDocument();
@$content->loadHTML( $html );
$itens = $content->getElementsByTagName( "span" );

$cotacao = null;

foreach( $itens as $item ) {

    if ( stripos( $item->getAttribute( "class"), "pr" ) !== false )
        $cotacao = str_replace( "1 USD = ", "", $item->nodeValue );
}

echo trim( $cotacao );
  • It’s extremely precarious, but it works well.

  • Hahaha, I found that the guy only post a photo and ask for code ready, but you took the wave picking the spans and looking for what has this class pr :)

  • 1

    is, the tricky of this "solution" is that changing the name of the classes, or the element, already stops working. So the ideal is to use an API.

  • I agree on gender, number and degree. This is one of those solutions that we do to break the branch once in a while, but you know you’ll have to change at some point.

Browser other questions tagged

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