Privacy Policy Eseguire php da un altro file php. Php asincrona - Daniele Carnovale
 

PHP18 Novembre 2015by Daniele Carnovale58Eseguire 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 —————————————-

 

58 comments

  • marie kondo

    22 Luglio 2016 at 11:16

    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.

    Reply

  • marie kondo

    22 Luglio 2016 at 11:16

    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.

    Reply

  • t shirts

    11 Agosto 2016 at 17:56

    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.

    Reply

    • Caiden

      7 Settembre 2016 at 19:20

      Hey, kiellr job on that one you guys!

      Reply

  • t shirts

    11 Agosto 2016 at 17:56

    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.

    Reply

    • Caiden

      7 Settembre 2016 at 19:20

      Hey, kiellr job on that one you guys!

      Reply

  • the best porn Xvideo selection

    27 Agosto 2016 at 12:33

    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!!

    Reply

    • Caro

      7 Settembre 2016 at 19:12

      You’ve imspsered us all with that posting!

      Reply

  • the best porn Xvideo selection

    27 Agosto 2016 at 12:33

    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!!

    Reply

    • Caro

      7 Settembre 2016 at 19:12

      You’ve imspsered us all with that posting!

      Reply

  • KashaYGaucin

    24 Settembre 2016 at 16:01

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

    Reply

  • KashaYGaucin

    24 Settembre 2016 at 16:01

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

    Reply

  • SoniaAYork

    26 Settembre 2016 at 0:17

    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.

    Reply

  • SoniaAYork

    26 Settembre 2016 at 0:17

    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.

    Reply

  • TashinaIGail

    29 Settembre 2016 at 21:15

    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.

    Reply

  • TashinaIGail

    29 Settembre 2016 at 21:15

    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.

    Reply

  • OtisHJaqua

    30 Settembre 2016 at 18:35

    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!

    Reply

  • OtisHJaqua

    30 Settembre 2016 at 18:35

    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!

    Reply

  • AdahRUccio

    3 Ottobre 2016 at 1:45

    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.

    Reply

  • AdahRUccio

    3 Ottobre 2016 at 1:45

    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.

    Reply

  • LanceKGelvin

    7 Ottobre 2016 at 8:59

    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.

    Reply

  • LanceKGelvin

    7 Ottobre 2016 at 8:59

    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.

    Reply

  • BradlyIFalor

    8 Ottobre 2016 at 22:22

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

    Reply

  • BradlyIFalor

    8 Ottobre 2016 at 22:22

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

    Reply

  • cheap mlb jerseys

    31 Ottobre 2016 at 22:30

    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!

    Reply

  • cheap mlb jerseys

    31 Ottobre 2016 at 22:30

    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!

    Reply

  • nike air zoom

    1 Novembre 2016 at 12:23

    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.

    Reply

  • nike air zoom

    1 Novembre 2016 at 12:23

    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.

    Reply

  • adidas stan smith shoes

    2 Novembre 2016 at 0:36

    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.

    Reply

  • adidas stan smith shoes

    2 Novembre 2016 at 0:36

    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.

    Reply

  • links of london

    2 Novembre 2016 at 15:55

    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.

    Reply

  • links of london

    2 Novembre 2016 at 15:55

    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.

    Reply

  • michael kors handbags clearance

    3 Novembre 2016 at 1:19

    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.

    Reply

  • michael kors handbags clearance

    3 Novembre 2016 at 1:19

    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.

    Reply

  • nike dunk shoes

    3 Novembre 2016 at 19:06

    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.

    Reply

  • nike dunk shoes

    3 Novembre 2016 at 19:06

    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.

    Reply

  • michael kors uk

    5 Novembre 2016 at 21:36

    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.

    Reply

  • michael kors uk

    5 Novembre 2016 at 21:36

    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.

    Reply

  • technique meditation

    8 Novembre 2016 at 10:28

    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.

    Reply

  • technique meditation

    8 Novembre 2016 at 10:28

    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.

    Reply

  • KipNHaeder

    15 Novembre 2016 at 3:52

    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.

    Reply

  • KipNHaeder

    15 Novembre 2016 at 3:52

    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.

    Reply

  • DianOHartley

    20 Novembre 2016 at 0:56

    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.

    Reply

  • DianOHartley

    20 Novembre 2016 at 0:56

    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.

    Reply

  • VedaMKaren

    20 Novembre 2016 at 4:19

    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.

    Reply

  • VedaMKaren

    20 Novembre 2016 at 4:19

    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.

    Reply

  • blacksluts

    1 Dicembre 2016 at 15:40

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

    Reply

  • blacksluts

    1 Dicembre 2016 at 15:40

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

    Reply

  • Johnc511

    13 Dicembre 2016 at 9:13

    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

    Reply

  • Johnc511

    13 Dicembre 2016 at 9:13

    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

    Reply

  • Smithf978

    13 Dicembre 2016 at 9:13

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

    Reply

  • Smithf978

    13 Dicembre 2016 at 9:13

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

    Reply

  • ThoraBMcniff

    18 Dicembre 2016 at 20:59

    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.

    Reply

  • ThoraBMcniff

    18 Dicembre 2016 at 20:59

    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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *