Blog

Eseguire php da un altro file php in modo asincrono, PHP Asincrona

Come eseguire php da un altro file php in modo asincrono. PHP asincrona

Spesso durante l’esecuzione di un file php è molto utile intercettare un evento e far partire in parallelo un altro processo, eseguendo un altro file php.

Ad esempio in alcuni casi si può essere utile separare alcuni sotto processi per velocizzare il caricamento delle pagine.

Oppure in alcuni casi bisogna eseguire del codice php, dispendioso e lento, di cui il risultato non è rilevante per la visualizzazione della pagina corrente o non serve nell’immediato.

Un altro esempio potrebbe essere quello dell’invio di un’email tramite un servizio di posta sul un server differente da quello in esecuzione del proprio file php.

Per tutti gli esempi descritti basta creare una chiamata php asincrona.

Per eseguire un file php in modo asincrono basta aprire un socket e simulare una chiamata esterna da browser al server. Tutto questo con una semplice funzione php ( JobStartAsync ) .

—————————— File phpasincrono.php —————————————-

//Parametri chiave-valore da inviare via post

$post = array(
"chiave1" =>'valore1',
"chiave2" => ' valore2 '
);

// la funzione riceve in input ip path e array post

// eventuali parametri get si possono aggiungere al path

JobStartAsync('192.168.0.1', '/phathweb/filedaeseguire.php',$post);

// l'esecuzione può continuare senza interruzione

function JobStartAsync ($server, $url, $post_array,$noasync=false, $port = 80, $conn_timeout = 60, $rw_timeout = 86400)
{

// echo 'JobStartAsync';

$errno = '';
$errstr = '';

set_time_limit(0);
$post_data = '';
foreach ($post_array as $k => $v) {
$post_data .= $k . '=' . $v . '&';
}
$fp = fsockopen($server, $port, $errno, $errstr, $conn_timeout);
if (!$fp) {

echo 'open socket errore';

return false;
}
$out = "POST " . $url . " HTTP/1.1\r\n";
$out .= "Host: $server\r\n";
$out .= "User-Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "Content-length: " . strlen($post_data) . "\r\n";
$out .= "Connection: close\r\n\r\n";
$out .= $post_data . "\r\n\r\n";

stream_set_blocking($fp, false);
stream_set_timeout($fp, $rw_timeout);
fwrite($fp, $out);

// per poter testare il funzionamento del file eseguito da chiamata socket

// bisogna restare in ascolto sul socket aperto e magari stampare la risposta

// così facendo la funzione non risulta più asincrona

if ($noasync) {
while (!feof($fp)) {
echo fgets($fp, 128);
}
}

fclose($fp);
return $fp;
}

—————————— fine File phpasincrono.php —————————————-

 

29 Comments

  1. marie kondo

    Hello there I am so grateful I found your web site,
    I really found you by error, while I was browsing on Aol for
    something else, Anyways I am here now and would just like to say thanks for a fantastic post and
    a all round interesting blog (I also love the theme/design), I don’t
    have time to look over it all at the moment but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep
    up the awesome jo.




    0



    0
  2. t shirts

    hello!,I love your writing so a lot! share we keep in touch more about your post on AOL?
    I need an expert on this space to solve my
    problem. Maybe that’s you! Having a look ahead to look you.




    0



    0
    1. Caiden

      Hey, kiellr job on that one you guys!




      0



      0
  3. the best porn Xvideo selection

    A motivating discussion is definitely worth comment. I think that you should write more on this issue, it might not be a taboo matter but generally people do not speak about these issues.
    To the next! All the best!!




    0



    0
    1. Caro

      You’ve imspsered us all with that posting!




      0



      0
  4. KashaYGaucin

    This article is truly a good one it helps new net users, that are wishing in favour of
    blogging.




    0



    0
  5. Maillot Juventus

    You know I love your blog!!!




    0



    0
  6. SoniaAYork

    Great post. I was checking constantly this blog and I’m impressed!
    Extremely helpful info specifically the past part :
    ) I maintain such information much. I had been seeking this certain info for any number of years.

    Many thanks and all the best.




    0



    0
  7. TashinaIGail

    What you said made a great deal of sense. However, what about this?
    what if you were to write a awesome title? I mean, I don’t want
    to tell you how to run your blog, however what if you added something that makes people desire
    more? I mean Eseguire php da un altro file php. Php asincrona – Daniele CarnovaleDaniele Carnovale is kinda
    boring. You should look at Yahoo’s home page and note how they
    write news titles to grab viewers to open the links. You might try adding a video or a related pic or two to grab
    readers interested about what you’ve written. Just my opinion, it would bring
    your blog a little bit more interesting.




    0



    0
  8. OtisHJaqua

    I tend not to even know the way I ended up here, however I assumed this publish was great.

    I will not know who you will be but definitely
    you happen to be going to a well known blogger for many who are certainly not already.
    Cheers!




    0



    0
  9. AdahRUccio

    Hi every one, here every person is sharing these know-how, so
    it’s pleasant to read this web site, and I used to pay a quick visit this webpage
    every day.




    0



    0
  10. LanceKGelvin

    What’s up to all, how is all, I think every one is getting
    more from this website, and your views are fastidious designed for new viewers.




    0



    0
  11. BradlyIFalor

    Asking them questions are genuinely fastidious thing when you are not understanding anything entirely, however this paragraph offers fastidious understanding yet.




    0



    0
  12. cheap mlb jerseys

    My spouse and i were quite fortunate Jordan managed to conclude his preliminary research from the ideas he had through the site. It is now and again perplexing to simply happen to be offering strategies that some people might have been making money from. And we all remember we now have the website owner to give thanks to for this. Most of the illustrations you have made, the simple web site menu, the relationships you can aid to create – it’s all wonderful, and it’s really helping our son in addition to our family believe that the subject is brilliant, and that is quite vital. Many thanks for everything!




    0



    0
  13. nike air zoom

    Needed to write you this very small word just to say thanks a lot as before considering the splendid thoughts you’ve contributed in this article. It was certainly pretty open-handed with you to make unhampered all some people could possibly have supplied for an e-book to make some dough on their own, notably considering the fact that you might have done it in the event you considered necessary. Those inspiring ideas also acted to become fantastic way to be sure that most people have the same passion the same as mine to know a whole lot more concerning this issue. I am certain there are some more pleasurable sessions ahead for individuals that read carefully your website.




    0



    0
  14. adidas stan smith shoes

    I and also my guys have already been looking at the nice things found on your site then before long developed a terrible feeling I had not expressed respect to the website owner for those tips. The young men were for that reason thrilled to see them and have in actuality been taking advantage of them. Thank you for genuinely considerably accommodating and for pick out these kinds of good issues millions of individuals are really wanting to know about. My personal honest regret for not saying thanks to sooner.




    0



    0
  15. links of london

    A lot of thanks for every one of your hard work on this website. My mother delights in conducting investigations and it is obvious why. A lot of people know all about the lively means you render invaluable thoughts by means of this blog and even increase contribution from others on this issue and our girl has been starting to learn a whole lot. Enjoy the rest of the year. You’re the one conducting a remarkable job.




    0



    0
  16. michael kors handbags clearance

    I simply wished to thank you so much once again. I’m not certain the things I could possibly have taken care of in the absence of the actual hints documented by you relating to this situation. This was the terrifying circumstance for me, however , noticing this well-written way you handled that forced me to leap with happiness. I’m happier for your work as well as hope that you are aware of a great job you were undertaking instructing people today by way of a web site. Most probably you’ve never encountered all of us.




    0



    0
  17. nike dunk shoes

    I would like to show appreciation to you for bailing me out of this type of trouble. Just after exploring throughout the world-wide-web and seeing principles that were not productive, I believed my life was well over. Living without the presence of strategies to the difficulties you have sorted out by way of the short article is a critical case, and those which might have in a negative way damaged my entire career if I had not come across the website. Your actual capability and kindness in maneuvering almost everything was tremendous. I don’t know what I would have done if I hadn’t come upon such a point like this. I can at this point look forward to my future. Thanks for your time very much for this professional and result oriented guide. I will not be reluctant to endorse your blog post to anyone who requires guidance on this subject matter.




    0



    0
  18. michael kors uk

    Thank you a lot for giving everyone such a brilliant chance to read critical reviews from this blog. It’s always very pleasing and stuffed with fun for me and my office colleagues to search your web site more than thrice per week to learn the new things you have. And of course, I’m so always pleased concerning the exceptional tricks you serve. Certain 1 points in this posting are indeed the most beneficial we have had.




    0



    0
  19. technique meditation

    Hi i am kavin, its my first time to commenting anyplace, when i read this post i thought i could
    also make comment due to this sensible paragraph.




    0



    0
  20. KipNHaeder

    Hello, all the time i used to check website posts here in the early hours in the morning, because i like to learn more and
    more.




    0



    0
  21. DianOHartley

    What’s up i am kavin, its my first time to commenting anywhere, when i read this
    piece of writing i thought i could also make comment due to this
    sensible paragraph.




    0



    0
  22. VedaMKaren

    I always used to study post in news papers however right now
    while i am an end user of net thus from now I
    am just using net for posts, due to web.




    0



    0
  23. blacksluts

    our nylon gams intertwined, my mitt resting easily on his nylon facialed meatpipe




    0



    0
  24. Johnc511

    I simply couldn’t depart your web site prior to suggesting that I actually loved the usual info a person supply for your guests? Is gonna be again continuously in order to check up on new posts. bbdefcddkgac




    0



    0
  25. Smithf978

    It’ll also save a lot of cash and time for those on a restricted budget who kbeabeedfekagdee




    0



    0
  26. ThoraBMcniff

    Hi, Neat post. There’s an issue along with your site in internet explorer, would check this?
    IE still may be the marketplace leader and an effective section of other people will leave out your fantastic writing because of
    this challenge.




    0



    0

Leave a Comment

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

This site uses Akismet to reduce spam. Learn how your comment data is processed.