$v) { if (!empty($string)) { $string .= '&'; } $string .= $k . '=' . urlencode($v); } } $ch = self::initCurl($url, $timeout); curl_setopt($ch, CURLOPT_FILE, null); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $string); $data = curl_exec($ch); $code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE); return $data; } /** * Download a file from a URL to file. * * @param string $target destination path to download file to * @param string $url URL of file to download * @param int $timeout timeout in seconds * @param ?int $code HTTP status code passed out by reference */ public static function toFile(string $target, string $url, int $timeout, ?int &$code): bool { $fh = fopen($target, 'wb'); if ($fh === false) ErrorHandler::traceError("Could not open $target for writing."); $ch = self::initCurl($url, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); curl_setopt($ch, CURLOPT_FILE, $fh); $res = curl_exec($ch); $code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE); fclose($fh); if ($res === false) { @unlink($target); return false; } return true; } }