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:  Laravel 5.8 pdf create Download and Print

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:  Angular 6 Animation Tutorial with Examples

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  DataTables Checkbox Check All Example
  2. Read Also:  Vue js Fancybox Popup Simple Example
  3. Read Also:  How to get out of an infinite loop using JavaScript?
  4. Read Also:  WooCommerce Get Product Tax, Shipping & Stock
  5. Read Also:  How to Set Headers in Laravel 6 Example?
  6. Read Also:  PHP cURL Multiple Asynchronous Http Requests
  7. Read Also:  How to get current url with or without query string using Vuejs
  8. Read Also:  Top 10 Yii Interview Questions and Answers for fresher
  9. Read Also:  PHP Laravel 5 Class Form HTML not found Resolve Error
  10. Read Also:  DataTables Checkbox Check All Example
CLOSEX