1

I am getting some data form an API using Curl, everything works fine and the data is parsed correctly. However this value that I'm parsing changes very regularly as its a counter.

I would like to refresh the call to update the new value every 2 or 3 seconds. How can I do this and where would I include this in Curl.php or the JS?

Curl

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'URL*(hidden)'
));
$headers = [
    'Accept: application/json',
    'Api-Key: (API KEY)',
    'Authorization: Basic (AUTH KEY)'
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
//print_r($resp);
echo $resp;

JS

    $(document).ready(function(){
    setInterval(function(){ 
        $.get("curldirectory.php", function(res){
          console.log(res.resource);
            var count = 1;
            var html1 = '';

            $(res.resource).each(function(i,value){

                if(value.VenueCode == "(MyCounter") {

                    html1+= '<div class="col-xs-12 hidden"><h1 class="bebas"> ' + addCommas(value.Total)  + '</h1></div>';

                }
                else{

                     html1+= '<div class="col-xs-12><h1 class="bebas"> ' + addCommas(value.Total)  + '</h1></div>';
                }

                count++;
            });

            $('#counter-list').html(html1);
        },'json');

            }, 3000);  
    });
</script>
5
  • You can wrap you $.get request in setInterval function automatically request after given time interval. Commented Apr 28, 2017 at 8:54
  • Hi, not sure I follow you mean wrap the whole $.get("curldirectory.php", function(res) function inside a setInterval function? Commented Apr 28, 2017 at 9:01
  • 1
    @user1673498 see w3schools.com/jsref/met_win_setinterval.asp. Try it, its quite simple. If you get stuck, edit the question with what you've done and we can help you fix it. Commented Apr 28, 2017 at 9:02
  • Hi i wrapped it inside the function you recommended but i t still didnt work..rather new to all this Commented Apr 28, 2017 at 9:09
  • "didn't work" doesn't describe your problem or current scenario. It doesn't help us to help you. Instead please be specific: do you get an error, or some unexpected behaviour? Have you done any basic debugging/logging to see if the various parts of the code are executing as you expect? Have you isolated where it is failing? Commented Apr 28, 2017 at 10:32

1 Answer 1

2

You're almost there.

function fetch_data(){
    $.ajax({
        url: 'curldirectory.php'
        type: 'get',
        success: function (data) {
            $.each($.parseJSON(data), function(){
                console.log(this.VenueCode)
                console.log(this.Total)
            });
        }
    });
}

setInterval(function() {
    fetch_data();
}, 3000);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.