53 lines
1.8 KiB
PHP
53 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace app\admin\service;
|
|
|
|
|
|
|
|
use think\facade\Log;
|
|
|
|
class SyncHttpService {
|
|
|
|
public static function post($url = "", $params = [], $header = []) {
|
|
empty($url) && apiErrMsg('地址不能为空');
|
|
$goUrl = strpos($url, 'http') !== false ? $url : config('domain') . "/index.php/" . $url;
|
|
Log::debug('SyncHttpService post url:'. $goUrl);
|
|
$info = parse_url($goUrl);
|
|
$host = $info['host'];
|
|
$port = $info['port'] ?? (strpos($goUrl, 'https') !== false ? 443 : 80);
|
|
$path = $info['path'];
|
|
$query = empty($params) ? '' : http_build_query($params);
|
|
$errno = 0;
|
|
$errstr = '';
|
|
$timeout = 300;
|
|
try {
|
|
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
|
|
$head = "POST $path HTTP/1.0\r\n";
|
|
$head .= "Host: $host\r\n";
|
|
$head .= "Content-type: application/x-www-form-urlencoded\r\n";
|
|
$head .= "Content-Length: " . strlen($query) . "\r\n";
|
|
$head .= "Connection:close\r\n";
|
|
if($header) {
|
|
foreach($header as $k => $v) {
|
|
$head .= "{$k}: {$v}\r\n";
|
|
}
|
|
}
|
|
$head .= "\r\n";
|
|
$head .= trim($query);
|
|
fwrite($fp, $head);
|
|
usleep(20000);
|
|
fclose($fp);
|
|
} catch (\Exception $exc) {
|
|
Log::debug('SyncHttpService error===');
|
|
Log::debug(compact('host', 'port', 'errno', 'errstr'));
|
|
Log::debug(compact('params', 'query'));
|
|
Log::debug('SyncHttpService error===');
|
|
writeLog($goUrl, 'SyncHttpServiceError');
|
|
writeLog(compact('host', 'port', 'errno', 'errstr'), 'SyncHttpServiceError');
|
|
writeLog(compact('params', 'query'), 'SyncHttpServiceError');
|
|
writeLog($exc, 'SyncHttpServiceError');
|
|
}
|
|
}
|
|
|
|
}
|