Home >Backend Development >PHP Tutorial >shell-php curl asynchronous request how to get results

shell-php curl asynchronous request how to get results

WBOY
WBOYOriginal
2016-10-17 09:30:232238browse

<code>
 function request($url, $payload) {

  $cmd = "curl -X POST -H 'Content-Type: application/json'";
  $cmd.= " -d '" . $payload . "' " . "'" . $url . "'";

  if (!$this->debug()) {
    $cmd .= " > /dev/null 2>&1 &";
  }

  //发送异步请求
  exec($cmd, $output, $exit);
  return $exit == 0;
}
?></code>

Reply content:

<code>
 function request($url, $payload) {

  $cmd = "curl -X POST -H 'Content-Type: application/json'";
  $cmd.= " -d '" . $payload . "' " . "'" . $url . "'";

  if (!$this->debug()) {
    $cmd .= " > /dev/null 2>&1 &";
  }

  //发送异步请求
  exec($cmd, $output, $exit);
  return $exit == 0;
}
?></code>

<code>$a=exec($cmd, $output, $exit);</code>

Can’t you use PHP’s curl function?

Using curl should not work, but you can call the callback at the request address.

You can use a network framework like swoole

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
$response = curl_exec($ch );

swoole can be asynchronous, you can still do it synchronously

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn