I'm trying to create a trading bot with PHP.
I would like to get the value of the currency and update it even without refreshing the page.
In order to do this I should execute this call every 5-10 seconds.
Is that correct?
$summ = $d->getMarketSummary("USDT-BTC");
Is there a way to do this asynchronously? Even if user doesn't reload the whole page.
I've heard of AJAX, but it's Javascript.
Thank you in advance.
You've heard it correctly, you need to do this in JavaScript, with AJAX. There are two parts for this:
1) You need to make an API in PHP, a route that will only respond with the data you want. So a page that when called:
<?php
$summ = $d->getMarketSummary("USDT-BTC");
echo $summ;
?>
And mapped to a url, let's say /data.
2) You need to make a JS in your page that calls that newly created route every-so-often; for that your need to use ajax (xmlhttprequest or Fetch API), and use the setInterval function to call it regularly and update the data in your page accordingly.
If you can't use javascript (AJAX) for this task, your only way is to create a CRON job that fires that PHP script every minute. Unfortunatelly, CRON jobs can't be configured to execute every X seconds, but you can fire it all minutes of the day.
Related
I want to implement the status log in textarea feature in my WordPress plugin. So I have a PHP variable which constantly updates and I want it to be displayed in the textarea in the admin panel.
I've tried to make a loop in JavaScript which asks server for changes of the variable every second, but as PHP is executing in blocking manner the server just ignores the request.
You can try create a function in your backend who gets all the information and return a HTML object and call this function using Javascript or whatever you wanna use for this call, after you get this html, just update the view.
Or if you prefer using Javascript, rather than returning a HTML, just return all data that you wanna show and update the view :D
I have a problem with project iam working on. In one route(program/messages) of my application, user can create and edit messages(using ckeditor textarea). These messages are saved in database. On another route(program/display) the application generates html site with messages(retrieved from database) created by user. The problem is that i need to update the display view(without site refresh ofc), when user change something in messages data(edit, or create new / delete). Any solution? Iam using codeigniter for backend.
Your view page must contain an ajax script. Which will check for database changes upon certain interval. That's all. I think ajax is new for you. Please grab a bit more AJAX concept. It's pretty handy ..
You can see W3school --
http://www.w3schools.com/ajax/default.asp
If you are in a hurry .. then ..
https://thenewboston.com/videos.php?cat=61
You can use
Jquery ajax() function
Javascript setInterval() function
Set the interval to certain time which will execute the ajax function to see if there is certain change in the database. if there is a change then update the view in success of ajax call.
There is another solution by using triggers in the database. But I am not quite sure about this.
I am making an online game using PHP and JavaScript, I have more knowledge in PHP than I do in JavaScript, though I am new in both languages, so keep that in mind.
So what I was trying to make in PHP / JavaScript and of course HTML was to refresh only the div or the area of code that I need, and I can't make the page reload every time that it gets new information or data because when the PHP is ran and done then I can't have anything else running, unless I was to use a loop though that sounds a bit sketchy and not sure if that's the method. I have tried: (PHP)
header("reload: 1");
Though that only refreshed the page, that is what I want to happen when I get data not always to be happening, so for example the program would get the information that someone is ready then it would send the client to another page as asll as the other client.
Though I would just like an explination if it is possible to only refresh a specific area when told to by example getting MySQL data.
function refresh_box()
{
$("#myDiv").load('path your PHP file');
setTimeout(refresh_box, 60000);
}
$(document).ready(function(){
refresh_box();
});
this setTimeout call your function for every 1 minute and load content dynamically in mydiv.
I have an AJAX call that is running a long PHP script where it has 20 different results, I would like to show when each step in the script is done.
Like so 1/20 done, 2/20 done, 3/20 done.
Latest 29-12-2015 03:17.
I tried to create the JSON file as so (jsonFileName_uniqueTimeStampHere.json) by PHP, but the time taken to create the file with PHP, result in a 404 file not found error!
Because when the AJAX call is running it comes to the progress call before the file has been created, I know I can't create the file with JavaScript but is there anyway to create.
The file before the success callback from jQuery AJAX?
What would be the best way to show progress information while AJAX call is running.
The way I have it now, I have a JSON file saved on the server that gets updated with the latest state that has completed, but if multiple users is running the same script the JSON file with the state gets overwritten.
Should I save the state of each progress in DB and then receive it with multiple calls to a PHP method that get state that has been completed?
Should I keep using the current method I use and add a userID to the JSON file so it is unique on each call to the file?
How should I go about doing it the same way as Seositecheckup?
What is the best way to make a progress with AJAX and PHP?
Please tell me if you need any more information.
I have looked around and don't feel like the info or half of info, there is to find online has been enough to do this myself.
I would like to use jQuery AJAX and not XMLHttpRequest, I'm looking for something similar to seositecheckup.com, when you scan a page you can see the state update on each completed function in the console and is done with different AJAX calls. How is that possible?
Should I forget about using jQuery and keep focus on plain JavaScript instead?
Right now I have a setup with jQuery that works the problem is, that I use a JSON file to get the result from and it gets overwritten when multiple users request the same script, is it possible to store the state in db instead and receive it from there with some unique identifier?
In the future I would like to make it possible to put the script into a queue that could be run and when the script ends it should send an e-mail to the user.
The HTTP way of handling requests that may take a long time is for requests to return a 202 and the body of the response should contain the URL where the user can query for the result.
#Request
POST /some/entitities
...
#Response
HTTP/1.0 202 Accepted
/jobs/{jobId}
The user can then poll /jobs/{jobId} which can return a number to represent progress. Do you have to use this? No, but if you do, others developers can immediately recognize what is going on.
Even if you don't use the approach I recommend, you will also have to keep track of job progress in your database and have a separate AJAX call to find out the current progress.
I know its a bit stupid question which is a bit meaningless but still I wanted to know is there any way to track last ajax call on page.
Actually the issue is I developed an application which works with ajax and many ajax calls happening with different different actions but if no ajax request call for 10 mins. it will expire session but I don't want it on just ajax call.
What I Want
if no activity happens in 10 mins. I don't wanna do anything but if even any click or keyboard activity happens then it will make 1 more ajax in after 10 mins. of click so session will not expire.
But that's not the issue to write script for it but the issue is I've lots of page and lots of different ajax call's so I just want to track last ajax call time on page.
I don't know if there's any possible way or not for it so I didn't write any code yet if any its possible then I'll start writing script otherwise I'll find another way but I appreciate if someone suggest me a way to do it.
Thanks
You can use ajaxsetup and increment a variable in ajaxsetup. Because ajaxsetup call in every ajax call
see the documentation of ajaxsetup http://api.jquery.com/jquery.ajaxsetup/
declare one variable and set 0 at the end of ajax request and change the value of variable as per time
i think this will help you
If you want your session not to expire, then increase its duration :
Adding this to your script will set php sessions lifetime to a month :
ini_set('session.gc_maxlifetime', 2678400);
ini_set('session.cookie_lifetime', 2678400);
EDIT for comment below :
To track last call add this at the beginning of your application entry point :
<?php
session_start();
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
$_SESSION['last_ajax_call'] = time();
}