在一台有很多IP的服务器,可以使用curl的interface参数,实现随机取一个IP访问页面

<?php
    function curl_get_contents($url) {
    $ips = file_get_contents('ips.txt'); // IP列表逗号分隔
    $ips_arr = explode(',', $ips); // 切成数组
    $sub = array_rand($ips_arr); // 随机返回一个Key值
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_INTERFACE, trim($ips_arr[$sub])); // 设定interface
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)");
    $retval = curl_exec($ch);
    curl_close ($ch);
    return $retval;
}

xi4oh4o
85 声望7 粉丝

ACGNM・OS X・Arch・Git・Vim・Laravel・Ruby・Python・Web・DevOps・❤ Like programming and frontend designing・Now: Responsible Moefou System operation and maintenance