tags:

views:

47

answers:

2

i have a task - i must grab some data from the URL. the link is http://cba.am. the data, i want to take, are in the some table, and i have the only one identifier, to reach my wanted data, it's the word "usd", which writes in that table(html)! i've written the following script, and it works! but i never heard how more experienced programers do such things, so i want to hear your comments.


here is script

<?php
    $str = file_get_contents("http://cba.am/");
    $key_usd = "USD";
    $sourse_usd_1 = explode($key_usd,$str);
    $usd1 = $sourse_usd_1[2];
    $sourse_usd_2=explode(">",$usd1);
    $usd2 = $sourse_usd_2[4];
    $sourse_usd_3=explode("<",$usd2);
    $usd = $sourse_usd_3[0];
?>

sorry for poor english:)

+1  A: 

I have used cURL for this in the past, you might be interested in reading more about it.

A key point to remember is that, if possible, you really should query a well-defined API (such as REST Web Service) instead of trying to scape a web page. This is because the format of data on a web page could change at any time, but an API will be more stable and your code will be less prone to break if anything is changed on the website you are pulling data from.

Justin Ethier
+2  A: 

Well, as long as the approach works for you (and they don't give you trouble for using it), it is fine. This technique is called "scraping". However, if they decide to change their site's structure, for example change the HTML tags or their position, your script will break, and you will have to update it. (You better have a mechanism in place to detect if the numbers don't make sense, so you can be warned.)

The much better and cleaner way would be to have them (the central bank, in that case) publish the data in a defined form, e.g. as a Web service, RSS feed or XML output that you can access.

Pekka
@Pekka how can they give me trouble for using this?
Syom
@Syom they can prohibit the automatic scraping of their web site, and the use of their data - most commercial currency services do this. If your call comes from the same IP every day (or every hour or whatever), and you (for example) publish the central bank's rates on your own web site, they could make trouble for you. I don't know - they are a government institution and the data may be copyright-free anyway. It also totally depends on what you are using this for.
Pekka
understand! thanks;)
Syom