Created
December 27, 2017 07:44
-
-
Save yusureabc/aa5b49e2fd01602f282f61b536205c42 to your computer and use it in GitHub Desktop.
PHP Fire and Forget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* https://www.eertime.com/archives/85.html | |
* [asyncExecute PHP异步执行任务] | |
* @param string $url 执行任务的url地址 | |
* @param array $post_data 需要post提交的数据POST | |
* @param array $cookie cookie数据用于登录等的设置 | |
* @return boole | |
*/ | |
function asyncExecute($url, $post_data = array(), $cookie = array()) { | |
$method = "GET"; | |
$url_array = parse_url($url); | |
$port = isset($url_array['port']) ? $url_array['port'] : 80; | |
$fp = fsockopen($url_array['host'], $port, $errno, $errstr, 30); | |
if (!$fp) { | |
return FALSE; | |
} | |
$getPath = isset($url_array['path']) ? $url_array['path'] : '/'; | |
if (isset($url_array['query'])) { | |
$getPath .= "?" . $url_array['query']; | |
} | |
if (!empty($post_data)) { | |
$method = "POST"; | |
} | |
$header = $method . " /" . $getPath; | |
$header .= " HTTP/1.1\r\n"; | |
$header .= "Host: " . $url_array['host'] . "\r\n"; | |
$header .= "Connection: Close\r\n"; | |
if (!empty($cookie)) { | |
$_cookie = strval(NULL); | |
foreach ($cookie as $k => $v) { | |
$_cookie .= $k . "=" . $v . "; "; | |
} | |
$cookie_str = "Cookie: " . base64_encode($_cookie) . " \r\n"; | |
$header .= $cookie_str; | |
} | |
if (!empty($post_data)) { | |
$_post = strval(NULL); | |
$atComma = ''; | |
foreach ($post_data as $k => $v) { | |
$_post .= $atComma . $k . "=" . $v; | |
$atComma = '&'; | |
} | |
$post_str = "Content-Type: application/x-www-form-urlencoded\r\n"; | |
$post_str .= "Content-Length: " . strlen($_post) . "\r\n"; | |
$post_str .= "\r\n".$_post . "\r\n"; | |
$header .= $post_str; | |
} | |
$header .= "\r\n"; | |
fwrite($fp, $header); | |
fclose($fp); | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://stackoverflow.com/questions/14587514/php-fire-and-forget-post-request | |
function curl_post_async($url, $params = array()) | |
{ | |
// create POST string | |
$post_params = array(); | |
foreach ($params as $key => &$val) | |
{ | |
$post_params[] = $key . '=' . urlencode($val); | |
} | |
$post_string = implode('&', $post_params); | |
// get URL segments | |
$parts = parse_url($url); | |
// workout port and open socket | |
$port = isset($parts['port']) ? $parts['port'] : 80; | |
$fp = fsockopen($parts['host'], $port, $errno, $errstr, 30); | |
// create output string | |
$output = "POST " . $parts['path'] . " HTTP/1.1\r\n"; | |
$output .= "Host: " . $parts['host'] . "\r\n"; | |
$output .= "Content-Type: application/x-www-form-urlencoded\r\n"; | |
$output .= "Content-Length: " . strlen($post_string) . "\r\n"; | |
$output .= "Connection: Close\r\n\r\n"; | |
$output .= isset($post_string) ? $post_string : ''; | |
// send output to $url handle | |
fwrite($fp, $output); | |
fclose($fp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment