PHP Curl发送post或get数据 并使用代理访问Socks5

直接上代码,以Socks5代理为例

public function curlfun1($url, $params = array(), $method = 'GET') {
    $header = array();
    $opts = array(
        CURLOPT_TIMEOUT => 10, 
        CURLOPT_RETURNTRANSFER => 1, 
        CURLOPT_SSL_VERIFYPEER => false, 
        CURLOPT_SSL_VERIFYHOST => false, 
        CURLOPT_HTTPHEADER => $header
    );
    /* 根据请求类型设置特定参数 */
    switch (strtoupper($method)) {
        case 'GET' :
        $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
        $opts[CURLOPT_URL] = substr($opts[CURLOPT_URL],0,-1);
        break;
    case 'POST' :
        //判断是否传输文件
        $params = http_build_query($params);
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = 1;
            $opts[CURLOPT_POSTFIELDS] = $params;
            break;
    default :
        break;
    }
    /* 初始化并执行curl请求 */
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); 
    curl_setopt($ch, CURLOPT_PROXY, "111.111.111.111:1080"); //Socks5 IP和端口
    curl_setopt($ch,CURLOPT_PROXYUSERPWD, "zhanghao:123456"); //Socks5 账号密码
    $data = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    if($error){
        $data = null;
    }        
    return $data;
}
0 条评论

赞助
会员