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

方法一:

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));


方法二:

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));




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSRF(Cross-Site Request Forgery)攻击是指攻击者通过伪造用户的身份,向服务器发送恶意请求。这种攻击可以导致多种问题,比如修改用户密码、发送恶意邮件等。为了防止这种攻击,许多网站会采用CSRF-Token验证方式。 PHP提供了curl库,可以用来模拟访问网站。下面是详解php curl带有csrf-token验证模拟提交方法的步骤: 1.首先,获取CSRF-Token。通常情况下,网站会在登录时生成并存储一个CSRF-Token。可以通过curl模拟登录,并从响应获取CSRF-Token。 2.使用curl发送POST请求时,需要在请求头添加CSRF-Token。可以使用curl提供的curl_setopt函数设置请求头的CSRF-Token。 3.发送POST请求时,需要将表单数据编码为URL格式。可以使用curl提供的curl_setopt函数设置POST数据,并使用http_build_query函数将数据编码为URL格式。 下面是一个示例代码: ``` // 模拟登录获取csrf-token $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://example.com/login'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['username' => 'test', 'password' => 'test'])); $result = curl_exec($ch); preg_match('/<meta name="csrf-token" content="(.*)">/', $result, $matches); $csrfToken = $matches[1]; // 模拟提交数据 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://example.com/submit'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['data' => 'test'])); curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-CSRF-Token: ' . $csrfToken]); $result = curl_exec($ch); ``` 在上面的代码,首先模拟登录获取csrf-token,然后模拟提交数据时,在请求头添加了CSRF-Token,并将表单数据编码为URL格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值