我想从文本文件中获取代理,然后将它们添加到我的数组中。我尝试了以下代码,但它不是取代码。
$path = './proxies.txt';
$proxies = array();
foreach(file($path, FILE_SKIP_EMPTY_LINES) as $line) {
$proxy = trim($line);
list($ip, $port) = explode(':', $proxy);
$proxies[$ip] = $port;
}
var_dump($proxies);
然后从数组中获取代理并使用
随机化它们curl_setopt($ch, CURLOPT_PROXY,$proxies[array_rand($proxies)]);
一些代码:
$path = './proxies.txt';
$proxies = array();
foreach(file($path, FILE_SKIP_EMPTY_LINES) as $line) {
$proxy = trim($line);
list($ip, $port) = explode(':', $proxy);
$proxies[$ip] = $port;
}
var_dump($proxies);
$ch = curl_init('https://www.website.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTP_VERSION,'CURL_HTTP_VERSION_1_1' );
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_ENCODING , "gzip,deflate");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_USERAGENT,$agents[array_rand($agents)]);
curl_setopt($ch, CURLOPT_PROXY,$proxies[array_rand($proxies)]);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
答案 0 :(得分:1)
我发现的最快方式是:
// Open the file $fp = @fopen($filename, 'r');
>
> // Add each line to an array if ($fp) { $array = explode("\n",
> fread($fp, filesize($filename))); } where $filename is going to be the
> path & name of your file, eg. ../filename.txt.
>
根据您设置文本文件的方式,您可能需要这样做 用\ n比特来玩。
答案 1 :(得分:0)
$handle = fopen("proxies.txt", "r");
$proxies = array();
if ($handle) {
while (($line = fgets($handle)) !== false) {
$proxies[explode(':', $line)[0]] = explode(':', $line)[1];
}
} else {
// error opening the file.
echo "Error: unable to open the file.";
}
fclose($handle);