HH\Asio\curl_exec

A convenience wrapper around curl_multi_await.

Description

function HH\Asio\curl_exec(
  mixed $urlOrHandle,
  bool $closeHandleIfHandle = false,
): Awaitable<string>

A convenience wrapper around curl_multi_await.

Pass a cURL handle, or, more simply, a string containing a URL (and the cURL handle will be created for you), and the cURL request will be executed via async and the string result will be returned.

curl_multi_info_read must be used to retrieve error information, curl_errno can't be used as this function is a wrapper to curl_multi_await.

Parameters

  • mixed $urlOrHandle - An existing cURL handle or a URL as a string. String URLs will create a default cURL GET handle.
  • bool $closeHandleIfHandle = false - Close cURL handle inside wrapper

Return Values

  • \Awaitable<string> - - An Awaitable representing the string result of the cURL request.

Examples

The following shows a scenario where you are going to wait for and return the result of cURL activity on URLs, using the convenient wrapper that is curl_exec.

<?hh

namespace Hack\UserDocumentation\API\Examples\ce;

async function get_curl_content(Set<string> $urls): Awaitable<Vector<string>> {
  $content = Vector {};
  foreach ($urls as $url) {
    $str = await \HH\Asio\curl_exec($url);
    $content[] = substr($str, 0, 10);
  }
  return $content;
}

function run(): void {
  $urls = Set {'http://www.google.com', 'http://www.cnn.com'};
  $content = \HH\Asio\join(get_curl_content($urls));
  var_dump($content);
}

run();
Output
object(HH\Vector)#8 (2) {
  [0]=>
  string(10) "<!doctype "
  [1]=>
  string(10) "<!DOCTYPE "
}