dongmu1989 2014-08-21 12:44 采纳率: 0%
浏览 32

从txt文件获取到数组? [重复]

This question already has an answer here:

I want to get proxies from a text file, and then add them to my array. I tried the following code, but it's not fetching the 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);

and then get proxies from array and randomising them using

curl_setopt($ch, CURLOPT_PROXY,$proxies[array_rand($proxies)]);

SOME OF THE CODE:

$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');
</div>
  • 写回答

2条回答 默认 最新

  • doob0526 2014-08-21 12:49
    关注

    The fastest way that I've found is:

    // Open the file $fp = @fopen($filename, 'r'); 
    > 
    > // Add each line to an array if ($fp) {    $array = explode("
    ",
    > fread($fp, filesize($filename))); } where $filename is going to be the
    > path & name of your file, eg. ../filename.txt.
    > 
    

    Depending how you've set up your text file, you'll have might have to play around with the bit.

    Found solution here: https://stackoverflow.com/a/8704656/3436942

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题