PHP cURL Multiple Asynchronous Http Requests

Today, We want to share with you PHP cURL Multiple Asynchronous Http Requests.In this post we will show you wordpress plugin require another plugin, hear for PHP Curl Examples – PHP cURL Post, Get, Header 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.

PHP cURL Multiple Asynchronous Http Requests

There are the Following The simple About PHP cURL Multiple Asynchronous Http Requests Full Information With Example and source code.

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

Also Read :  How to Open URL in New Tab using Angular.js 2

PHP: Setting cURL timeout options.

<?php

$url = 'http://localhost/tester/log.php';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);
$data =  json_decode($response,true);

if(curl_errno($ch)){
    throw new Exception(curl_error($ch));
} else {
	print_r($data);
}

PHP: Set custom headers with cURL

$url = 'http://localhost/test/file.php';
$ch = curl_init($url);

$customHeaders = array(
    'Accept-Encoding: gzip, deflate, br',
    'Cookie: PHPSESSID=BBRP012565Iidjkd8965;',
    'Referer: https://www.google.com/'
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $customHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($ch);
$data =  json_decode($response,true);

if(curl_errno($ch)){
    throw new Exception(curl_error($ch));
} else {
	print_r($data);
}

Using sleep to add delay or increase execution time

<?php
echo date('H:i:s');
sleep(15);
flush();
echo "<br>";
echo date('H:i:s');
?>

sleep(30);

php curl_exec stopped

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
curl_setopt($ch, CURLOPT_URL, "https://myurl"); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "post_field1=value1"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FAILONERROR, false); 
$server_output = curl_exec ($ch); // <------ IT DIES HERE INSIDE 
 curl_close ($ch); 
return $server_output; 

Multiple asynchronous http calls using curl

Single curl request

<?php

$t1 = microtime(true);

function fetchContent($Url) {
	if (!function_exists('curl_init')){
	die('Sorry cURL is not installed!');
	}
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $Url);
	curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
	curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}

echo "<pre style='width:100%;height:40%'>";
echo fetchContent('https://your-domain-name.com/api/admin/php/products/fetched-product/fetch1.php');
echo "</pre>";

echo '<hr>';

echo "<pre style='width:100%;height:40%'>";
echo fetchContent('https://your-domain-name.com/api/admin/php/products/fetched-product/fetch2.php');
echo "</pre>";

echo 'fetched for: ' . (microtime(true) - $t1) . "\n";

multiple http requests using curl

<?php

if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}

$ch = array();
$mh = curl_multi_init();
$total = 100;

$t1 = microtime(true);

$URLs = array( "https://your-domain-name.com/api/admin/php/products/fetched-product/fetch2.php",
"https://your-domain-name.com/api/admin/php/products/fetched-product/fetch2.php");

$i = 0;
foreach($URLs as $url) {
$ch[$i] = curl_init();
curl_setopt($ch[$i], CURLOPT_URL, $url);
curl_setopt($ch[$i], CURLOPT_HEADER, 0);
curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true);

curl_multi_add_handle($mh, $ch[$i]);
$i ++;
}

$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
//usleep(100); 
} while ($active);

$content = array();

$i = 0;
foreach ($ch AS $i => $c) {
$content[$i] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}

curl_multi_close($mh);

echo "<pre style='width:100%;height:40%'>";
echo $content[0];
echo "</pre>";

echo '<hr>';

echo "<pre style='width:100%;height:40%'>";
echo $content[1];
echo "</pre>";

echo 'fetched for: ' . (microtime(true) - $t1) . "\n";

PHP Sleep Functions

while (true) {
    $getProducts = CurlPage($YOUR_URL);
    $getPage1httpCode = $getProducts['httpCode'];
    if ($getPage1httpCode === 200) {
        $DATA = $getProducts['Data'];
        break;
    }
    sleep(rand(5, 10));
    $tracks++;
    if ($tracks === 5) {
        die('Sorry, Fatal Error');
    }
}

Web Programming Tutorials Example with Demo

Read :

Also Read :  Simple React Hello World Example

Summary

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

I hope you get an idea about PHP cURL Multiple Asynchronous Http Requests.
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.

Leave a Reply

avatar
  Subscribe  
Notify of