+ 2
Get content of a site php
I want to get some contents of other site in php and display it I have tried regex but the site is too complex to be gotten by regex
26 Respuestas
+ 1
Note you shouldn't use file_get_contents() but use streams instead to load a website. However, this gives a quick demo:
<?php
// Get the info first.
$yobit_info = file_get_contents("https://yobit.net/api/3/info");
sleep(2);
// Decode it (it's in JSON format to start with)
$data = json_decode($yobit_info);
// Get the pairs.
$pairs = $data->pairs;
// For each pair, do...
foreach ($pairs as $currency_name => $object)
{
// Get the currency data.
$currency_data = json_decode(file_get_contents("https://yobit.net/api/3/ticker/$currency_name"));
print_r($currency_data);
sleep(2);
exit;
}
?>
Note, I added the 'exit', so only the first currency is displayed for my demo. Otherwise, it would thrash their server for no good reason. They stipulate you should put a two second pause in between fetches (hence the sleep(2) calls).
Any problems, let me know.
+ 2
Which site? Simple web scraper methods are better. Regex breaks too easily on websites.
+ 2
thanks Xan
I will try that
+ 2
the demo has worked fine
+ 2
now I understand somehow
but I don't know what cron is 😅
I will google it and try
+ 2
what the time will I need to get my app update?
I mean the difference between the real update in yobit and my app
+ 1
I'm not sure if you can get the whole table. But you can get one currency with "ticker" method
This is for litecoin
https://yobit.net/api/2/ltc_btc/ticker
And I think this is for bitcoint
https://yobit.net/api/2/btc_usd/ticker
You could loop all currencies through and make a request to that method.
+ 1
thanks
but I get no result in my page
+ 1
I have tried the github file index.php
but nothing happened
+ 1
how?
+ 1
could you give me simple example code that gets the ticker for one coin
+ 1
but if I try to get all table it will destroy my app
+ 1
it is impossible getting the whole table
it is too long
+ 1
Why is it impossible? What is happening? Is your browser timing out?
I would run a cronjob on a unix-based web server, that accesses the whole table over time, and then creates an up to date table 30 minutes or something like that.
It is still possible to scrape the HTML probably too. But the API that Mohamed mentioned, is the best way to go / alongisde the cron job.
Note, that even their own website struggles to display the market! That's not a good sign.
What is you high level objective?
+ 1
I need to be up to date in my app
it will not be good if I get the table in along time
it gives me too many requiest error
+ 1
That's why you need to set up a cron job on your web server, to get the data via their API, without overloading their server. You then cache the most recent request of the data (which will now reside on your web server), and that is the copy displayed to your website quickly.
+ 1
A cron job is a piece of code, or a script, which is automatically scheduled by a computer's operating system (Unix-based systems) at specific times. There is an equivalent for Windows systems too.
+ 1
2 minutes per request. So, how many API requests are needed? It's 1 (to get the info), then one more for each currency request. How many 'pairs' (currencies) were there?
+ 1
then it will get ages to get full table
+ 1
they have limitation of 100 requests per minutes
so could I get 100 coins data in a minute ?