最新消息:本站访问过程中有任何问题请与我联系……

使用php的curl类,检测socket5、Http代理服务器可用性

技术教程 xx2008 2658浏览

设计初衷当然是用php curl bind不一个sokcet5去访问一个随机页面(公网)这个页面的功能就是通过一个md5来验证sokcet5服务器是否真的可用
A服务器通过 socket5发出http://www.xxorg.com/a.php?t=randkey
B服务器,echo md5($_GET[‘t’] . mykey);
A服务器拿到content以后也用相同算法算一个$resut看看是不是等于B服务器的echo值,如果一样,就是success 不一样就是failed

1.第一个文件curl.inc.php
10.     *                    ’host’ => ”,
11.     *                   ‘header’ => ”,
12.     *                   ‘method’ => ”,
13.     *                   ‘referer’ => ”,
14.     *                   ‘cookie’ => ”,
15.     *                   ‘post_fields’ => ”,
16.     *                    ['login' => '',]
17.     *                    ['password' => '',]
18.     *                   ‘timeout’ => 0
19.     *                   );
20.     */
21.    public function init($params)
22.    {
23.                                if($this->ch==0)
24.                        $this->ch = curl_init();
25.        $user_agent = ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9′;
26.        $header = array(
27.        "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
28.        "Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3",
29.        "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7",
30.        "Keep-Alive: 300");
31.        if (isset($params[‘host’]) && $params[‘host’])      $header[]="Host: ".$params[‘host’];
32.        if (isset($params[‘header’]) && $params[‘header’]) $header[]=$params[‘header’];
33. 
34.        @curl_setopt ( $this -> ch , CURLOPT_RETURNTRANSFER , 1 );
35.        @curl_setopt ( $this -> ch , CURLOPT_VERBOSE , 1 );
36.        @curl_setopt ( $this -> ch , CURLOPT_HEADER , 1 );
37.        if(strlen($params[‘proxy’])>0) curl_setopt($this->ch, CURLOPT_PROXY,$params[‘proxy’]);
38.        if(strlen($params[‘proxyport’])>0) curl_setopt($this->ch, CURLOPT_PROXYPORT, $params[‘proxyprot’]);  
39.        curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);     
40.        if ($params[‘method’] == "HEAD") @curl_setopt($this -> ch,CURLOPT_NOBODY,1);
41.        @curl_setopt ( $this -> ch, CURLOPT_FOLLOWLOCATION, 1);
42.        @curl_setopt ( $this -> ch , CURLOPT_HTTPHEADER, $header );
43.        if ($params[‘referer’])    @curl_setopt ($this -> ch , CURLOPT_REFERER, $params[‘referer’] );
44.        @curl_setopt ( $this -> ch , CURLOPT_USERAGENT, $user_agent);
45.        if ($params[‘cookie’])    @curl_setopt ($this -> ch , CURLOPT_COOKIE, $params[‘cookie’]);
46. 
47.        if ( $params[‘method’] == "POST" )
48.        {
49.            curl_setopt( $this -> ch, CURLOPT_POST, true );
50.            curl_setopt( $this -> ch, CURLOPT_POSTFIELDS, $params[‘post_fields’] );
51.        }
52.        @curl_setopt( $this -> ch, CURLOPT_URL, $params[‘url’]);
53.        @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYPEER, 0 );
54.        @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYHOST, 0 );
55.        if (isset($params[‘login’]) & isset($params[‘password’])) 56.            @curl_setopt($this -> ch , CURLOPT_USERPWD,$params[‘login’].‘:’.$params[‘password’]);
57.        @curl_setopt ( $this -> ch , CURLOPT_TIMEOUT, $params[‘timeout’]);
58.    }
59. 
60.    /**
61.     * Make curl request
62.     *
63.     * @return array  ’header’,'body’,'curl_error’,'http_code’,'last_url’
64.     */
65.    public function exec()
66.    {
67.        $response = curl_exec($this->ch);
68.        $error = curl_error($this->ch);
69.        $result = array( ‘header’ => ”,
70.                         ‘body’ => ”,
71.                         ‘curl_error’ => ”,
72.                         ‘http_code’ => ”,
73.                         ‘last_url’ => ”);
74.        if ( $error != "" )
75.        {
76.            $result[‘curl_error’] = $error;
77.            return $result;
78.        }
79. 
80.        $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE);
81.        $result[‘header’] = $this->pass_header(substr($response, 0, $header_size));
82.        $result[‘body’] = substr( $response, $header_size );
83.        $result[‘http_code’] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE);
84.        $result[‘last_url’] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL);
85.                                $result[‘last_sent’]=curl_getinfo($this->ch,CURLINFO_HEADER_OUT );
86.        return $result;
87.    }
88.    function __destruct(){
89.        @curl_close($this->ch);
90.    }
91.public function pass_header($header)
92.        {
93.                $result=array();
94.                $varHader=explode("\r\n", $header);
95.                if(count($varHader)>0)
96.                        {
97.                                for($i=0;$i
108.第二个文件
109. "http://xxxxx.com/a.php?md=$key",   //自己找个服务器放个程序!
116.                       ‘host’ => ‘help.dhgate.com’,
117.                       ‘proxy’ => "$proxy:$prot",
118.                       ‘proxyport’=>"$prot",
119.                       ‘timeout’ =>10 
120.                       );
121.$GLOBALS[‘curl’]->init($params);
122.$result=$GLOBALS[‘curl’]->exec();
123.if(trim($result[‘body’])==md5($key.‘my’))
124.        return true;
125.else
126.        return false;
1.}
128.echo verify(‘localhost’,’7777′);
129.?>
130.目标服务器的那个程序。。。
131.

verify 函数返回的true false标志socket5服务器是否可用! curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5); 这行话,是专门给socket5用的你要验证http proxy的也一样,删了这行就行了!

zp8497586rq

转载请注明:服务器VPS折腾 » 使用php的curl类,检测socket5、Http代理服务器可用性