CURL-php的经典例子

阅读次数: 972

  • A+
所属分类:PHP PHP 语言学习
CURL-php的经典例子

限制IP访问


<?php
$client_ip = getip();
$referer = getreferer();
$allow_ip = '192.168.1.100';
$allow_referer = 'http://www.csdn.net';
if($client_ip==$allow_ip && strpos($referer, $allow_referer)===0){
echo 'allow access';
}else{
echo 'deny access';
}
// 获取访问者ip
function getip(){
if(!empty($_SERVER['HTTP_CLIENT_IP'])){
$cip = $_SERVER['HTTP_CLIENT_IP'];
}elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$cip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}elseif(!empty($_SERVER['REMOTE_ADDR'])){
$cip = $_SERVER['REMOTE_ADDR'];
}else{
$cip = '';
}
return $cip;
}
// 获取访问者来源
function getreferer(){
if(isset($_SERVER['HTTP_REFERER'])){
return $_SERVER['HTTP_REFERER'];
}
return '';
}
?>


CURL模拟 IP 访问


<?php
function doCurl($url, $data=array(), $header=array(), $referer='', $timeout=30){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
// 模拟来源
curl_setopt($ch, CURLOPT_REFERER, $referer);
$response = curl_exec($ch);
if($error=curl_error($ch)){
die($error);
}
curl_close($ch);
return $response;
}
// 调用
$url = 'http://www.example.com/server.php';
$data = array();
// 设置IP
$header = array(
'CLIENT-IP: 192.168.1.100',
'X-FORWARDED-FOR: 192.168.1.100'
);
// 设置来源
$referer = 'http://www.csdn.net/';
$response = doCurl($url, $data, $header, $referer, 5);
echo $response;
?>


模拟session访问


header("Content-Type:text/html;charser=UTF-8");

function get_curl($url, $cookie = 0, $post = 0, $referer = 0, $header = 0, $ua = 0, $nobaody = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$klsf[] = "Accept:*/*";
$klsf[] = "Accept-Encoding:gzip,deflate,sdch";
$klsf[] = "Accept-Language:zh-CN,zh;q=0.8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $klsf);
if ($post) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if ($header) {
curl_setopt($ch, CURLOPT_HEADER, TRUE);
}

curl_setopt($ch, CURLOPT_COOKIE, $cookie);
if ($referer) {
if ($referer == 1) {
curl_setopt($ch, CURLOPT_REFERER, "http://m.qzone.com/infocenter?g_f=");
} else {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
}
if ($ua) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0');
}
if ($nobaody) {
curl_setopt($ch, CURLOPT_NOBODY, 1);//主要头部
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//跟随重定向
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;

}

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: