1

I want to know how do I make my ajax request run in crontab? I have here an ajax request that gets data from an api, for each data I'm executing ajax post request to update a row in my database.

How do I do this in Curl?

here is my script,

function getDataFs()
{
    var _token = $("input[name='_token']").val();
    $.ajax({
        url: 'https://url/api/employees', 
        type: 'GET',
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        },
        success: function(response){  
            console.log(response);
            for(i=0; i < response.length; i++)
            {
                $.ajax({
                    url: '/api/update_employees_list',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    type: 'POST',
                    dataType: 'json',
                    data: { 'employee_data': response[i], '_token': _token },
                    success: function(response){
                        console.log(response);
                    }
                })
            }   
        }
    });     
}   

I'm also passing request headers and xhr fields,

I hope someone can help me with this, If not possible on ajax , can you help me accomplish this on Curl or any possible solution for this, Thank You!

1
  • check my answer, its like prototype for your problem Commented Dec 18, 2017 at 11:50

2 Answers 2

1

you can do something like following and add this file in the Crontab.

//getting all initial response
$raw_response = doCurl('https://url/api/employees', false, 'GET');
$response = json_decode($raw_response);

//iterate them
foreach ($response as $value) {
    //set your headers
    $headers = array();
    //set your post data
    $post_arr = array();
    //make your sub requests.
    doCurl($url, $headers, 'POST', $post_arr);
}


function doCurl($url, $headers, $method = 'GET', $post_arr = array()){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

    if ($method === 'POST') {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_arr);
    }

    if (!empty($headers)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }

    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $res = curl_exec($ch);
    curl_close($ch);
    return $res;
}
Sign up to request clarification or add additional context in comments.

Comments

0

AJAX is a technology that works only in your browser, but it is based on regular HTTP requests. Using the Firefox developer console, you can simply copy an executed AJAX request to a cURL call. This can be used in your cronjob

1 Comment

can you show me a demo or a working sample for that, Im still confused, can you show me a link or a draft, thank you!

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.