好吧,在我拥有的脚本中,我想与另一台服务器通信并从服务器检索文件。通常我没有任何cURL选项指定要使用的IP地址(接口),但如果服务器知道建立的连接是在188.138.110.125(我的IP地址)和服务器之间,则只允许我下载。所以我编辑我的脚本看起来像这样:
<?php
$user = 'username';
$pass = 'password';
$cookie_file = './sessions/cookie.txt';
// exit if the script is called directly
if ( empty($url) )
die();
$url = "http://www.example.com/file.zip";
// checking if the login session is valid
$logged_in = 0;
if ( file_exists($cookie_file) )
{
$cookie = file_get_contents($cookie_file);
$ch = curl_init();
$options = array(
CURLOPT_URL => 'https://www.example.com/testfile.zip&'.$cookie_file,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_INTERFACE => '188.138.110.125',
CURLOPT_SSL_VERIFYPEER => 0
);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
$logged_in = !preg_match('~ERROR~', $content);
}
// logging in and storing the cookie
if ( !$logged_in )
{
$ch = curl_init();
$options = array(
CURLOPT_URL => 'https://example.com/login.php?login='.urlencode($user).'&password='.urlencode($pass).'&withcookie=1',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_INTERFACE => '188.138.110.125',
CURLOPT_SSL_VERIFYPEER => 0
);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
preg_match('~cookie=([0-9A-F]+)~', $content, $match);
if ( empty($match[1]) )
die('Error: login failed');
$cookie = "enc={$match[1]}";
file_put_contents($cookie_file, $cookie);
}
//----------------------- THIS IS WHERE PROBLEM HAPPENS ---------------------------
// checking if the direct link to the file is available
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 1,
CURLOPT_INTERFACE => '188.138.110.125',
CURLOPT_RETURNTRANSFER => 1
);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
preg_match('~Location: ([^\s]+)~', $content, $match);
// if the direct link to the file was not found
if ( empty($match[1]) )
// display any other errors
if ( preg_match('~(ERROR: [^\n]+)~', $content, $match) )
die("Error {$match[1]}");
else
die('Error: undefined error (1)');
$direct_link = $match[1];
// extracting filename
$pieces = explode('/', $direct_link);
$filename = end($pieces);
// extrating the size of the file
$ch = curl_init();
$options = array(
CURLOPT_URL => $direct_link,
CURLOPT_HEADER => 1,
CURLOPT_NOBODY => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_INTERFACE => '188.138.110.125',
CURLOPT_SSL_VERIFYPEER => 0
);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
preg_match('~Content-Length: (\d+)~', $content, $match);
$filesize = $match[1];
// setting headers for the user
header('Content-Type: application/octet-stream');
header("Content-Disposition: filename=$filename");
header("Content-Length: $filesize");
// printing the file
$ch = curl_init();
$options = array(
CURLOPT_URL => $direct_link,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_BINARYTRANSFER => 1,
CURLOPT_INTERFACE => '188.138.110.125',
CURLOPT_COOKIE => $cookie
);
curl_setopt_array($ch, $options);
curl_exec($ch);
curl_close($ch);
?>
但现在它在下载文件时会下载一个0字节的文件。文件名是准确的,文件的扩展名也是如此,它没有大小。此问题仅在我添加CURLOPT_INTERFACE =&gt;时开始'188.138.110.125',进入剧本。非常感谢任何帮助,并提前感谢您。
注意:如果我继续尝试下载,在完全随机的时间它会实际找到合适的文件大小,但如果我再次这样做,我就没有运气。