Skip to content

Instantly share code, notes, and snippets.

@Schnitzel
Last active February 28, 2019 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schnitzel/ed65648571f25647eb0bc7b2e5a1b998 to your computer and use it in GitHub Desktop.
Save Schnitzel/ed65648571f25647eb0bc7b2e5a1b998 to your computer and use it in GitHub Desktop.
<?php
$url = "https://www.example.com";
$options = array(
CURLOPT_RETURNTRANSFER => true, // don't echo page
CURLOPT_VERBOSE =>true,
);
$cm = curl_multi_init();
// step 1 of 3 (adding requests)
//request 1
$ch1 = curl_init( $url );
$verboseH1 = fopen('php://temp', 'w+');
$options[CURLOPT_STDERR] = $verboseH1;
curl_setopt_array( $ch1, $options);
curl_multi_add_handle($cm, $ch1);
//request 2
$ch2 = curl_init( $url );
$verboseH2 = fopen('php://temp', 'w+');
$options[CURLOPT_STDERR] = $verboseH2;
curl_setopt_array( $ch2, $options);
curl_multi_add_handle($cm, $ch2);
//request 3
$ch3 = curl_init( $url );
$verboseH3 = fopen('php://temp', 'w+');
$options[CURLOPT_STDERR] = $verboseH3;
curl_setopt_array( $ch3, $options);
curl_multi_add_handle($cm, $ch3);
// step 2 of 3 (executing requests in parallel)
// for more information about these strange loops , check http://php.net/manual/en/function.curl-multi-init.php#115055
$active = null;
do {
$mrc = curl_multi_exec($cm, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($cm) == -1) {
usleep(100);
}
do {
$mrc = curl_multi_exec($cm, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
// step 3 of 3 (getting results)
echo "<h1>Requet 1</h1>\n";
echo "errmsg1: '" . curl_error($ch1) . "'\n";
rewind($verboseH1);
echo "verbose1:\n" . stream_get_contents( $verboseH1) . "\n";
echo "===============================================================\n";
echo "<h1>Requet 2</h1>\n";
echo "errmsg2: '" . curl_error($ch2) . "'\n";
rewind($verboseH2);
echo "verbose2:\n" . stream_get_contents( $verboseH2) . "\n";
echo "===============================================================\n";
echo "<h1>Requet 3</h1>\n";
echo "errmsg3: '" . curl_error($ch3) . "'\n";
rewind($verboseH3);
echo "verbose3:\n" . stream_get_contents( $verboseH3) . "\n";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment