您的位置:首页 > 编程语言 > PHP开发

PHP中使用curl及代理IP模拟post提交【两种实用方法】

2017-10-25 16:36 896 查看
方法一:

[php] view
plain copy

function request_by_other($remote_server,$post_string){  

    $context = array(    

    'http'=>array(    

        'proxy'=>'127.0.0.1:8787', // 代理IP  

        'request_fulluri'=>true, // 是否使用代理  

        'method'=>'POST',    

        'header'=>'Content-type: application/x-www-form-urlencoded'."\r\n".    

            'User-Agent : Jimmy\'s POST Example beta'."\r\n".    

            'Content-length: '.strlen($post_string)+8,    

        'content'=>'article_id='.$post_string)    

    );    

    $stream_context = stream_context_create($context);    

    $data = @file_get_contents($remote_server,FALSE,$stream_context);    

    return $data;    

}   

  

$remote_server = "http://www.xxxxx.com/article/yj/views.jhtml";  

$post_string = "10898";  

  

var_dump(request_by_other($remote_server, $post_string));  

方法二:

[php] view
plain copy

function curl_string ($url,$user_agent,$proxy){  

    $ch = curl_init();  

    if (!empty($proxy)) { // 有代理IP就使用代理  

        curl_setopt ($ch, CURLOPT_PROXY, $proxy); // 使用代理IP  

        // 伪造客户端来源IP  

        $xforip = rand(1, 254).".".rand(1, 254).".".rand(1, 254).".".rand(1, 254);  

        curl_setopt ($ch, CURLOPT_HTTPHEADER, array('CLIENT-IP:'.$xforip, 'X-FORWARDED-FOR:'.$xforip));  

    }  

    curl_setopt ($ch, CURLOPT_URL, $url);  

    curl_setopt ($ch, CURLOPT_POSTFIELDS, 'article_id=10898');  

    curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent); //模拟用户使用的浏览器  

    curl_setopt ($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."\cookie.txt");  

    // curl_setopt ($ch, CURLOPT_HEADER, 1); //输出头信息  

    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);  

    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转  

    curl_setopt ($ch, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer  

    curl_setopt ($ch, CURLOPT_TIMEOUT, 120);  

    $result = curl_exec ($ch);  

    curl_close($ch);  

  

    return $result;  

  

}  

  

$url_page = "http://www.xxxxxx.com/article/yj/views.jhtml";  

$user_agentArray = array(  

    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36",  

);  

  

$proxyArray = array(  

    "http://125.39.68.180:80",  

    "http://111.206.81.248:80",  

);  

  

$proxy = empty($proxyArray) ? "" : $proxyArray[rand(0, count($proxyArray) - 1)];  

$user_agent = $user_agentArray[rand(0, count($user_agentArray) - 1)];  

  

var_dump(curl_string($url_page,$user_agent, $proxy));  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: