-
加入我們
-
VIP定制
-
求關注
Thank you for visiting
029-81773686
全網(wǎng)開發(fā)&整合營銷服務商
CopyRight
?HOOBOO All rights reserved
2020.08.14
我們創(chuàng)建了一個基于fsockopen的函數(shù),這個函數(shù)中利用fsockopen去訪問url,但是在訪問時,并不要求獲取url顯示的內(nèi)容,而是僅僅發(fā)出訪問請求,請求到達后馬上關閉這個訪問。這樣做的好處就是無需再等待被訪問的url是否返回了可靠的信息,節(jié)約了時間,這段代碼的執(zhí)行時間在0.1-0.2秒之間,對于普通訪客而言,幾乎察覺不到。因此,在使用時,僅需要調(diào)用這個函數(shù)和對應的url即可。不過,這里并沒有提供數(shù)據(jù)傳輸?shù)牟糠?,如何傳輸?shù)據(jù),其實只需要在$header中增加post的內(nèi)容即可。
/**
* @PHP異步執(zhí)行任務
* @param string $url 執(zhí)行任務的url地址
* @param array $post_data 需要post提交的數(shù)據(jù)POST
* @param array $cookie cookie數(shù)據(jù)用于登錄等的設置(此處內(nèi)部調(diào)用,無需鑒權)
* @return boole
*/
function asynchronous($url,$post_data = array(), $debug = false)
{
$url_array = parse_url($url);
if($debug){
echo '<pre>';
print_r($url_array);
echo '</pre>';
}
//用fsockopen()嘗試連接
$fp = fsockopen($url_array['host'], 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
//建立成功后,向服務器寫入數(shù)據(jù)
$getPath = isset($url_array['path']) ? $url_array['path'] : '/';
$out = "GET /".$getPath."/ HTTP/1.1\r\n";
$out .= "Host:".$url_array['host']."\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
/*忽略執(zhí)行結(jié)果
if($debug){
echo '<pre>';
while (!feof($fp)) {
print_r(fgets($fp, 128));
}
echo '</pre>';
}
//關閉鏈接
fclose($fp);
}
}
// 調(diào)用方法
asynchronous('https://www.hooboo.cn/admin/index/test',['name'=>'haha']);
// 直接返回結(jié)果
echo '操作成功';
CopyRight ? 西安宏博網(wǎng)絡科技有限公司 備案號:陜ICP備10007014號-8 站點地圖 免責聲明:本網(wǎng)站部分資源來源于網(wǎng)絡,如有侵權,請聯(lián)系我們告知刪除,我們將會盡快處理,謝謝!本站不承擔任何法律責任。