How to Make Async Requests in PHP

Today, We want to share with you How to Make Async Requests in PHP.In this post we will show you How to make asynchronous requests in php with example, hear for How to post an asynchronous HTTP request in PHP we will give you demo and example for implement.In this post, we will learn about Building a REST client with asynchronous calls using PHP and curl with an example.

How to Make Async Requests in PHP

There are the Following The simple About PHP – Async cURL Requests Full Information With Example and source code.

Read Also:  Global.asax File in ASP.NET MVC Tutorial with Example

As I will cover this Post with live Working example to develop php curl without waiting for response, so the is php synchronous or asynchronous is used for this example is following below.

download & install Guzzle php package.

run in CMD using Terminal

php composer.phar require guzzlehttp/guzzle:~6.0

//or

composer require guzzlehttp/guzzle:~6.0

Example 1:

include autoload file in the script

<?php

require_once(__DIR__ . '/vendor/autoload.php');
$api_client = new GuzzleHttp\Client();

$promises = [
    $api_client->getAsync('https://your_domain_name')->then(function ($output) { echo 'first'; }),
    $api_client->getAsync('https://stackoverflow.com')->then(function ($output) { echo 'second'; }),
    $api_client->getAsync('https://www.google.com')->then(function ($output) { echo 'third'; }),
    $api_client->getAsync('https://your_domain_name')->then(function ($output) { echo 'fourth'; }),
    $api_client->getAsync('https://your_domain_name')->then(function ($output) { echo 'fifth'; }),
    $api_client->getAsync('https://www.phpclasses.org')->then(function ($output) { echo 'six'; }),
    $api_client->getAsync('https://your_domain_name')->then(function ($output) { echo 'seven'; }),
    $api_client->getAsync('https://your_domain_name')->then(function ($output) { echo 'eight'; }),
    $api_client->getAsync('https://your_domain_name')->then(function ($output) { echo 'nine'; }),
];

$results = GuzzleHttp\Promise\unwrap($promises);

// some time Wait for the http guzzle requests to done, even if some of them fail
$results = GuzzleHttp\Promise\settle($promises)->wait();

print "good Luck finished." . PHP_EOL;

Web Programming Tutorials Example with Demo

Read :

Read Also:  How to Check ruby Version by CLI and File?

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about php curl wait for response.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.