In some cases, we need to use CURL to simulate requests to complete something. But the reality is always somewhat different from the ideal. The other party's server may have some restrictions on the source information requested. How should we deal with this situation? This article will tell you how to let curl disguise source information to deceive the server's source information restrictions.
[one.php]
<?php $post_data = array ( "user" => "admin", "pwd" => "123456" ); $header_ip = array( 'CLIENT-IP:8.8.8.8', 'X-FORWARDED-FOR:8.8.8.8', ); $referer='http://www.liangshao.com'; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, 'http://localhost/curl/two.PHP'); //伪造来源referer curl_setopt ($ch,CURLOPT_REFERER,$referer); //伪造来源ip curl_setopt($ch, CURLOPT_HTTPHEADER, $header_ip); //提交post传参 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //加上这个表示执行curl_exec是把输出做为返回值,不会输出到浏览器 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $out_put=curl_exec ($ch); curl_close ($ch); echo $out_put;
[two.php]
<?php //请求来源referer echo '[HTTP_REFERER]<br>'; echo $_SERVER['HTTP_REFERER']; //请求来源ip //[注]此处的IP打印顺序是目前很多开源系统的IP获取顺序 echo '<hr>[IP]<br>'; echo $_SERVER['HTTP_CLIENT_IP']; echo '<br>'; echo $_SERVER['HTTP_X_FORWARDED_FOR']; echo '<br>'; echo $_SERVER['REMOTE_ADDR']; //POST数据 echo '<hr>[POST]<br><pre class="brush:php;toolbar:false">'; var_dump($_POST); echo '';
Related reading;
php CURL Get cookies to simulate login method code example
php curl error troubleshooting method detailed explanation
PHP curl simulates logging into a website with verification code
The above is the detailed content of PHP curl disguise source information. For more information, please follow other related articles on the PHP Chinese website!