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

本文介绍两种使用PHP发起网络请求的方法,一种是通过file_get_contents结合stream_context_create实现,另一种是利用cURL进行请求。这两种方法均能实现POST请求,并且支持设置代理服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一:

[php]  view plain  copy
  1. function request_by_other($remote_server,$post_string){  
  2.     $context = array(    
  3.     'http'=>array(    
  4.         'proxy'=>'127.0.0.1:8787'// 代理IP  
  5.         'request_fulluri'=>true, // 是否使用代理  
  6.         'method'=>'POST',    
  7.         'header'=>'Content-type: application/x-www-form-urlencoded'."\r\n".    
  8.             'User-Agent : Jimmy\'s POST Example beta'."\r\n".    
  9.             'Content-length: '.strlen($post_string)+8,    
  10.         'content'=>'article_id='.$post_string)    
  11.     );    
  12.     $stream_context = stream_context_create($context);    
  13.     $data = @file_get_contents($remote_server,FALSE,$stream_context);    
  14.     return $data;    
  15. }   
  16.   
  17. $remote_server = "http://www.xxxxx.com/article/yj/views.jhtml";  
  18. $post_string = "10898";  
  19.   
  20. var_dump(request_by_other($remote_server$post_string));  


方法二:

[php]  view plain  copy
  1. function curl_string ($url,$user_agent,$proxy){  
  2.     $ch = curl_init();  
  3.     if (!empty($proxy)) { // 有代理IP就使用代理  
  4.         curl_setopt ($ch, CURLOPT_PROXY, $proxy); // 使用代理IP  
  5.         // 伪造客户端来源IP  
  6.         $xforip = rand(1, 254).".".rand(1, 254).".".rand(1, 254).".".rand(1, 254);  
  7.         curl_setopt ($ch, CURLOPT_HTTPHEADER, array('CLIENT-IP:'.$xforip'X-FORWARDED-FOR:'.$xforip));  
  8.     }  
  9.     curl_setopt ($ch, CURLOPT_URL, $url);  
  10.     curl_setopt ($ch, CURLOPT_POSTFIELDS, 'article_id=10898');  
  11.     curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent); //模拟用户使用的浏览器  
  12.     curl_setopt ($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."\cookie.txt");  
  13.     // curl_setopt ($ch, CURLOPT_HEADER, 1); //输出头信息  
  14.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);  
  15.     curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转  
  16.     curl_setopt ($ch, CURLOPT_AUTOREFERER, 1 ); // 自动设置Referer  
  17.     curl_setopt ($ch, CURLOPT_TIMEOUT, 120);  
  18.     $result = curl_exec ($ch);  
  19.     curl_close($ch);  
  20.   
  21.     return $result;  
  22.   
  23. }  
  24.   
  25. $url_page = "http://www.xxxxxx.com/article/yj/views.jhtml";  
  26. $user_agentArray = array(  
  27.     "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36",  
  28. );  
  29.   
  30. $proxyArray = array(  
  31.     "http://125.39.68.180:80",  
  32.     "http://111.206.81.248:80",  
  33. );  
  34.   
  35. $proxy = empty($proxyArray) ? "" : $proxyArray[rand(0, count($proxyArray) - 1)];  
  36. $user_agent = $user_agentArray[rand(0, count($user_agentArray) - 1)];  
  37.   
  38. var_dump(curl_string($url_page,$user_agent$proxy));  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值