$v) { if (!empty($string)) { $string .= '&'; } $string .= $k . '=' . urlencode($v); } } $ch = self::initCurl($url, $timeout, $head); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $string); $data = curl_exec($ch); $head = self::getContents($head); if (preg_match_all('#^HTTP/\d+\.\d+ (\d+) #m', $head, $out)) { $code = (int) array_pop($out[1]); } else { $code = 999; } 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 * @return boolean */ public static function toFile($target, $url, $timeout, &$code) { $fh = fopen($target, 'wb'); if ($fh === false) Util::traceError("Could not open $target for writing."); $ch = self::initCurl($url, $timeout, $head); curl_setopt($ch, CURLOPT_FILE, $fh); $res = curl_exec($ch); $head = self::getContents($head); fclose($fh); if ($res === false) { @unlink($target); return false; } if (preg_match_all('#^HTTP/\d+\.\d+ (\d+) #m', $head, $out)) { $code = (int) array_pop($out[1]); } else { $code = '999 ' . curl_error($ch); } return true; } }