Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6549895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:08:59+00:00 2026-05-25T12:08:59+00:00

I’d like to run a particularly expensive fql query in the background, log results

  • 0

I’d like to run a particularly expensive fql query in the background, log results to the database, and retrieve it later without the user having to wait for each step.

Can you share an example of how to run a facebook request asynchronously?

main.php

$uid = $facebook->getUser();
if ($uid) {
    try {
        echo $user;

        ////////////////////////////////////////////////////////
        // Run lengthy query here, asynchronously (async.php) //
        ////////////////////////////////////////////////////////
        //                                                    //
        // For example: $profile = $facebook->api('/me');     //
        // (I know this request doesn't take long, but        //
        // if I can run it in the background, it'll do.       //
        //                                                    //
        ////////////////////////////////////////////////////////


    } catch (FacebookApiException $e) {
        echo $e;
    }
}

async.php

$profile = $facebook->api('/me');
$run = mysql_query("INSERT INTO table (id) VALUES (" . $profile['id'] . ");";

complete.php

echo getProfileId(); // assume function grabs id from db, as stored via async.php
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:08:59+00:00Added an answer on May 25, 2026 at 12:08 pm

    My solution to running PHP jobs in the background is just to have the script itself make a new request which executes the actual job.

    The code I’ve used before is the following, I’m not sure if there are more elegant solutions… but it has worked more than well enough for me in the past. The reason I use fsock and not file_get_contents() etc is of course so that I won’t have to wait for the job to finish (which would defeat the purpose)

    $sock = fsockopen("127.0.0.1", 80);
    fwrite($sock, "GET /yourjoburl/ HTTP/1.1\r\n");
    fwrite($sock, "Host: yourdomain.com\r\n");
    fwrite($sock, "Connection: close\r\n");
    fwrite($sock, "\r\n");
    fflush($sock);
    fclose($sock);
    

    So, then you just have the other script write the results and progress to a database or whatever… also remember that MySQL supports mutexes, which means you can easily prevent multiple jobs from running at the same time… or to allow other scripts to wait for the job to finish.

    PS. The reason I personally avoid exec and all that stuff is that it just seems like a hassle to work with, different servers, different setups, different OSes, etc. This works the same on all hosts that allow you to open sockets. Although you might want to add a private key to the request that you verify in the job, or check the IP of the caller, to prevent others from being able to start jobs.

    EDIT: This is untested but should work if you want to forward the cookies as well, including the session cookie.

    $sock = fsockopen("127.0.0.1", 80);
    fwrite($sock, "GET /yourjoburl/ HTTP/1.1\r\n");
    fwrite($sock, "Host: yourdomain.com\r\n");
    fwrite($sock, "Cookie: " . $_SERVER['HTTP_COOKIE'] . "\r\n");
    fwrite($sock, "Connection: close\r\n");
    fwrite($sock, "\r\n");
    fflush($sock);
    fclose($sock);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
Let's say I'm outputting a post title and in our database, it's Hello Y’all
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to count the length of a string with PHP. The string
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.