curl setopt(interface)

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

<?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;
}
相关文章
相关标签/搜索