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

shell-php curl asynchronous request how to get results

WBOY
Release: 2016-10-17 09:30:23
Original
2256 people have browsed it

<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template