php 检测socks代理

check_proxy.php

<!--
check proxy socks4 and socks5
Copyright by mimvp.com
2015-02-09
-->

<?php
    define('PROXY_CONF', '124.160.35.2:808');
    define('PROXY_CHECK_URL', 'http://www.baidu.com/');
    define('PROXY_LOGPATH', dirname(__FILE__) . '/proxy.log');
    
    function check() {
    	$ch = curl_init();
    
    	curl_setopt($ch, CURLOPT_PROXY, PROXY_CONF);
    	curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
    	curl_setopt($ch, CURLOPT_URL, PROXY_CHECK_URL);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
    	$response = curl_exec($ch);
        $error = curl_error($ch);

        var_dump("<br>response: ".$response);
        var_dump("<br>eror: ".$error);
    
    	$tpl = "%s | %s\n";
    	$time = date('Y-m-d H:i:s');
    	if(curl_errno($ch)){
    		@file_put_contents(PROXY_LOGPATH, sprintf($tpl, $time, 'PROXY_NOT_WORKING ERROR:'.curl_error($ch)), FILE_APPEND);
    	} else {
    		@file_put_contents(PROXY_LOGPATH, sprintf($tpl, $time, 'PROXY_IS_WORKING'), FILE_APPEND);
    	}
    	curl_close($ch);
    	unset($ch);
    }
    check();
?>

 

php 利用socks代理

<?php
	/*
	check proxy socks4 and socks5
	Copyright by mimvp.com
	2015-02-09
	*/

	$curl = curl_init();

	curl_setopt($curl, CURLOPT_PROXY, "59.37.163.176:1080");
	curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);	// 使用SOCKS5, 还有SOCKS4	
	curl_setopt($curl, CURLOPT_URL, "http://www.mimvp.com");
	curl_setopt($curl, CURLOPT_HEADER, 1);
	curl_setopt($curl, CURLOPT_TIMEOUT, 30);
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_POST, 1);
	$data = array('user' => "mimvp.com", 'password' => 'mimvp-proxy');
	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	//curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 1);			// 如果是HTTP代理
	//curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');		// cookie你懂的
	$request = curl_exec($curl);
	var_dump("request : ".$request);

	curl_close($curl);
	unset($curl)
?>