+ 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

23rd Apr 2018, 2:59 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
26 Respostas
+ 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.
25th Apr 2018, 4:16 AM
Emma
+ 2
Which site? Simple web scraper methods are better. Regex breaks too easily on websites.
23rd Apr 2018, 3:05 PM
Emma
+ 2
thanks Xan I will try that
25th Apr 2018, 4:22 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 2
the demo has worked fine
25th Apr 2018, 4:25 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 2
now I understand somehow but I don't know what cron is šŸ˜… I will google it and try
25th Apr 2018, 6:52 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 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
25th Apr 2018, 7:20 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 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.
23rd Apr 2018, 3:27 PM
Toni Isotalo
Toni Isotalo - avatar
+ 1
thanks but I get no result in my page
23rd Apr 2018, 3:38 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 1
I have tried the github file index.php but nothing happened
23rd Apr 2018, 3:48 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 1
how?
23rd Apr 2018, 4:34 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 1
could you give me simple example code that gets the ticker for one coin
25th Apr 2018, 3:43 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 1
but if I try to get all table it will destroy my app
25th Apr 2018, 4:26 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 1
it is impossible getting the whole table it is too long
25th Apr 2018, 4:53 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 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?
25th Apr 2018, 5:33 AM
Emma
+ 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
25th Apr 2018, 5:37 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 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.
25th Apr 2018, 6:44 AM
Emma
+ 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.
25th Apr 2018, 7:10 AM
Emma
+ 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?
25th Apr 2018, 7:31 AM
Emma
+ 1
then it will get ages to get full table
25th Apr 2018, 7:32 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 1
they have limitation of 100 requests per minutes so could I get 100 coins data in a minute ?
25th Apr 2018, 7:33 AM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar