php强大的curl功能,网站开发技术
作者:广州小程序开发 时间:2018-03-09 16:56
Php的curl功能非常强大,通过它可以从服务器端模拟一个http请求,例如抓取网页、模拟登陆等。声明:本文只用于技术交流。
假如我们有个a.php页面 代码如下:
<?php
//php模拟头部信息
header("Content-Type:text/html; charset=utf-8");
$ch = curl_init();
$url = "http://localhost/curl/b.php";
$header = array(
'CLIENT-IP:58.68.44.61', //模拟ip
'X-FORWARDED-FOR:58.68.44.61',
'Referer:http://www.daliuren.net/', //模拟来源
'REMOTE_ADDR:58.68.44.61'
);
// 发送参数数组
$data = array (
'message_type' =>'message',
'message_name' => 'test',
'message_birthday' => '30',
'message_xingbei' =>'114455527',
'message_address' =>'测试留言内容',
'message_tel' =>'120120',
'message_riqi' =>'121212',
'message_title' =>'121212',
'message_riqi' =>'121212',
'message_ip' =>'58.68.44.61',
'act' =>'add'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$page_content = curl_exec($ch);
curl_close($ch);
echo $page_content;
?>
b.php文件代码
<?php
//echo $_POST[' message_address ']."<BR>";
echo $_SERVER['HTTP_REFERER']."<br>";
;
echo getenv('HTTP_CLIENT_IP');
echo getenv('HTTP_X_FORWARDED_FOR');
echo getenv('REMOTE_ADDR');
echo $_SERVER['REMOTE_ADDR'];
?>
通过实例我们可以看到$_SERVER['HTTP_REFERER']和HTTP_CLIENT_IP被模拟了,但是$_SERVER['REMOTE_ADDR']还是127.0.0.1
假如我们有个a.php页面 代码如下:
<?php
//php模拟头部信息
header("Content-Type:text/html; charset=utf-8");
$ch = curl_init();
$url = "http://localhost/curl/b.php";
$header = array(
'CLIENT-IP:58.68.44.61', //模拟ip
'X-FORWARDED-FOR:58.68.44.61',
'Referer:http://www.daliuren.net/', //模拟来源
'REMOTE_ADDR:58.68.44.61'
);
// 发送参数数组
$data = array (
'message_type' =>'message',
'message_name' => 'test',
'message_birthday' => '30',
'message_xingbei' =>'114455527',
'message_address' =>'测试留言内容',
'message_tel' =>'120120',
'message_riqi' =>'121212',
'message_title' =>'121212',
'message_riqi' =>'121212',
'message_ip' =>'58.68.44.61',
'act' =>'add'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$page_content = curl_exec($ch);
curl_close($ch);
echo $page_content;
?>
b.php文件代码
<?php
//echo $_POST[' message_address ']."<BR>";
echo $_SERVER['HTTP_REFERER']."<br>";
;
echo getenv('HTTP_CLIENT_IP');
echo getenv('HTTP_X_FORWARDED_FOR');
echo getenv('REMOTE_ADDR');
echo $_SERVER['REMOTE_ADDR'];
?>
通过实例我们可以看到$_SERVER['HTTP_REFERER']和HTTP_CLIENT_IP被模拟了,但是$_SERVER['REMOTE_ADDR']还是127.0.0.1