When POST data using CURL request it’s posted in the foreground so the request needs to wait until finish the request. Using below code no need to wait until completed API request, it’s working in the background PHP.
The background process works using “fsockopen” function of PHP.
Follow the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php // $params['parameter1']="value1"; $params['parameter2']="value2"; $url="http://localhost/weanswer.xyz/send_data"; foreach ($params as $key => &$val) { if (is_array($val)) $val = implode(',', $val); $post_params[] = $key . '=' . urlencode($val); } $post_string = implode('&', $post_params); $parts = parse_url($url); $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30); $out = "POST " . $parts['path'] . " HTTP/1.1\r\n"; $out.= "Host: " . $parts['host'] . "\r\n"; $out.= "Content-Type: application/x-www-form-urlencoded\r\n"; $out.= "Content-Length: " . strlen($post_string) . "\r\n"; $out.= "Connection: Close\r\n\r\n"; if (isset($post_string)) $out.= $post_string; fwrite($fp, $out); fclose($fp); ?> |
More Stories
Text to speech
image upload s3 via PHP CURL
Mollie Payment – PHP Laravel