Pregunta

I have searched in a lot of places and didn't find any solution for my problem properly.. I have a PHP code that is taking about 1 minute to retrieve the informations, becouse it's fetching urls in a Open Graph php, so it has to open a lot of sites, find the meta datas (like: title, images, descriptions) and echo it for me... So, the whole system is working fine, but every time an user enters the site the php code has to do all the work again retrieving the meta datas and stuff...

I need to create a system that run the PHP in the background server and store the information, so when the user enter the site, all the work would be already done, showing only the results... any idea?

I did this test:

After the 1 min waiting and with the page already open showing the results, I entered in the Source Code, copied all the content and create a new .html page (copy and pasting the code generated), and of course everything was transformed in a simple html code showing the php results... And everything loaded in a sec, becouse all the Open Graph process was already done.. That is what I need, but automatically..

(english is not my main language, so, you know) :)

¿Fue útil?

Solución

Add the results to a database or cached file. Have a cron script which runs the script that takes a long time, but modify the script to save the output to the file or database. Then, have your user-facing script simply retrieve the contents of the file/database.

Otros consejos

There are many options to do this.  Some depend on how you want users to get the information.

 1. If you're on Windows, you could try using:

    $WShell = new COM("WScript.Shell");
    $path = ($_SERVER['DOCUMENT_ROOT']."/long_task.php");
    $exec = $WShell->Run("php.exe -f $path", 0, false);


Have the long_task.php update the database or something to let you know you've completed the task and that the user can retrieve the updates.

 2. Load the data into an iframe


    <!-- Send the data request to the iframe -->
    <form action="long_task.php" target="ifr">
    ...
    </form>

    <!-- put iframe somewhere on your main page -->
    <iframe name="ifr" id="ifr" >
    </iframe>

 3. Also, you may wish to consider using javascript instead.  A jQuery asynchronous request to the long_task.php file can be created using something like:

    <!-- HTML code -->
    <a href="post_data()">Button</a>

    <!-- jquery code -->
       function post_data() {
           var dataString = $('#data_to_send1, #data_to_send2).serialize();
           $.post(url, dataString, function(data) { 
                       //callback details
               //use js-jquery to display your data here
           });
       }

Have you ever used memcached?? Facebook and all big sites use this and it's pretty easy to implement.

http://memcached.org/

Good luck with that!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top