php fsockopen函数发送post,get请求获得网页内容(反防采集)

 更新时间:2016年11月25日 16:50  点击:1373
 代码如下 复制代码
$post =1;
$url = parse_url($url);
$host ='http://www.111cn.net';
$path ='/';
$query ='?action=111cn.net';
$port =80;
 
if($post) {
  $out = "post $path http/1.0 ";
  $out .= "accept: */* ";
  //$out .= "referer: $boardurl ";
  $out .= "accept-language: zh-cn ";
  $out .= "content-type: application/x-www-form-urlencoded ";
  $out .= "user-agent: $_server[http_user_agent] ";
  $out .= "host: $host ";
  $out .= 'content-length: '.strlen($post)." ";
  $out .= "connection: close ";
  $out .= "cache-control: no-cache ";
  $out .= "cookie: $cookie ";
  $out .= $post;
 } else {
  $out = "get $path http/1.0 ";
  $out .= "accept: */* ";
  //$out .= "referer: $boardurl ";
  $out .= "accept-language: zh-cn ";
  $out .= "user-agent: $_server[http_user_agent] ";
  $out .= "host: $host ";
  $out .= "connection: close ";
  $out .= "cookie: $cookie ";
 }
 $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
 if(!$fp)
 {
  return '';//note $errstr : $errno
 } else {
  return '成功访问';
 }
 /*
 fsockopen语法
 
 resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )


 
 启动一个套接字连接到指定的主机的资源。
    php支持在互联网领域的目标和unix在所支持的套接字传输列表说明。所支持的传输列表也可以检索使用stream_get_transports()。
 
 该插座预设会被启用,阻塞模式。你可以切换到非阻塞模式使用stream_set_blocking()。
 
 如果上面实例看不懂,就来看个简的吧


*/

 

 代码如下 复制代码
$fp = fsockopen("www.111cn.net", 80, $errno, $errstr, 30);
 if (!$fp) {
  echo "$errstr ($errno)<br /> ";
 } else {
  $out = "get / http/1.1 ";
  $out .= "host: www.111cn.net ";
  $out .= "connection: close ";
  fwrite($fp, $out);
  while (!feof($fp)) {
   echo fgets($fp, 128);
  }
  fclose($fp);
 }
如果你要判断一些函数是不是可以用我们可以使用function_exists函数来检测哦,下面我们来看几个例子,希望对你会有帮助。

语法
bool function_exists ( string $function_name )
检查的定义的函数的列表,同时内置(内部)和用户定义的,为function_name。
返回值

返回true,如果function_name存在,是一个函数,否则返回false。
*/

if (function_exists('imap_open')) {
    echo "imap functions are available.www.111cn.net<br /> ";
} else {
    echo "imap functions are not available.<br /> ";
}


//function_exists returns false on null and empty string:

if (function_exists('')) {
                echo "empty string function exists ";
        }

        if (function_exists(null)) {
                echo "null function exists ";
        }
  
//如果您使用suhosin.executor.func.blacklist而不是在你的php.ini disabled_functions,function_exists将返回true为功能。我用这个有与suhosin.executor.func.blacklist和disabled_functions相同beahviour:

function suhosin_function_exists($func) {
    if (extension_loaded('suhosin')) {
        $suhosin = @ini_get("suhosin.executor.func.blacklist");
        if (empty($suhosin) == false) {
            $suhosin = explode(',', $suhosin);
            $suhosin = array_map('trim', $suhosin);
            $suhosin = array_map('strtolower', $suhosin);
            return (function_exists($func) == true && array_search($func, $suhosin) === false);
        }
    }
    return function_exists($func);
}

//需求:在所有连接后面添加一个request=xxx; 这个函数比preg_replace灵活性更强,要注意它所替换的内容为整个正则表达式的内容。
$content = '<a href="http://www.111cn.net/aaa.php">链接1</a><a href="http://www.111cn.net/aaa.php?id=111">链接2</a>';
function add_source($matches)
{
    if(strpos($matches[1], '?'))
    {
        return 'href="'.$matches[1].'&request=xxx"';  //注意,这里和下面都加上了正则里括号外的东西:href="
    }
    else
    {
        return 'href="'.$matches[1].'?request=xxx"';
    }
}
$content = preg_replace_callback('/href=['|"](.*?)['|"]/', 'add_source', $content);


//实例二


  // 此文本是用于 2002 年的,
  // 现在想使其能用于 2003 年
  $text = "april fools day is 04/01/2002 ";
  $text.= "last christmas was 12/24/2001 ";

  // 回调函数
  function next_year($matches) {
    // 通常:$matches[0] 是完整的匹配项
    // $matches[1] 是第一个括号中的子模式的匹配项
    // 以此类推
    return $matches[1].($matches[2]+1);
  }

  echo preg_replace_callback(
              "|(d{2}/d{2}/)(d{4})|",
              "next_year",
              $text);

  // 结果为:
  // april fools day is 04/01/2003
  // last christmas was 12/24/2002

//preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) 主题为匹配搜索模式,替换替换
/*
要搜索的模式。它可以是一个字符串或一个字符串数组。

电子修饰符使preg_replace函数()替代治疗后,适当引用作为参数是php教程代码进行替换。提示:请确保置换构成一个有效的php代码字符串,否则php将抱怨在包含preg_replace函数线()解析错误。

返回值

preg_replace函数()返回一个数组,如果这个问题的参数是一个数组或一个字符串,否则。

如果找到匹配,新问题会产生,否则将返回主题不变或null如果发生错误。
*/

//实例一

 代码如下 复制代码
$string = 'april 15, 2003';
$pattern = '/(w+) (d+), (d+)/i';
$replacement = '${1}1,$3';
echo preg_replace($pattern, $replacement, $string);

//实例二

 代码如下 复制代码
$string = 'the quick brown fox jumped over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);

//通过ksorting模式和替代,我们应该得到我们想要的。

 代码如下 复制代码
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);

//更换几个值

 代码如下 复制代码
$patterns = array ('/(19|20)(d{2})-(d{1,2})-(d{1,2})/',
                   '/^s*{(w+)}s*=/');
$replace = array ('//', '$ =');
echo preg_replace($patterns, $replace, '{startdate} = 1999-5-27');

//过滤所有html 标签

 代码如下 复制代码
preg_replace("/(</?)(w+)([^>]*>)/e",
             "'\1'.strtoupper('\2').'\3'",
             $html_body);

//过滤所有script代码

 代码如下 复制代码

$user_agent = "mozilla/4.0 (compatible; msie 5.01; windows nt 5.0)";

$ch = curl_init();    // initialize curl handle
curl_setopt($ch, curlopt_url, $url); // set url to post to
curl_setopt($ch, curlopt_failonerror, 1);              // fail on errors
curl_setopt($ch, curlopt_followlocation, 1);    // allow redirects
curl_setopt($ch, curlopt_returntransfer,1); // return into a variable
curl_setopt($ch, curlopt_port, 80);            //set the port number
curl_setopt($ch, curlopt_timeout, 15); // times out after 15s

curl_setopt($ch, curlopt_useragent, $user_agent);

$document = curl_exec($ch);

$search = array('@<script[^>]*?>.*?</script>@si',  // strip out javascript教程 www.111cn.net
'@<style[^>]*?>.*?</style>@siu',    // strip style tags properly
'@<[/!]*?[^<>]*?>@si',            // strip out html tags
'@<![ss]*?–[ ]*>@',         // strip multi-line comments including cdata
'/s{2,}/',

);

$text = preg_replace($search, " ", html_entity_decode($document));

$pat[0] = "/^s+/";
$pat[2] = "/s+$/";
$rep[0] = "";
$rep[2] = " ";

$text = preg_replace($pat, $rep, trim($text));

return $text;
}

/*
此函数接受一个url并返回页面的纯文本版本。它使用curl来检索网页,一个正则表达式的组合,以去除所有不必要的空白。这个功能甚至剥夺了从形式和script标记,这是由php函数忽略,如用strip_tags(他们地带唯一的标记文本,留下完整的文字在中间)。

正则表达式被分为两个阶段,以避免删除单(也由 s的匹配)回车,但仍然删除所有空白行和多个换行符或空格,修整手术进行了2个阶段进行。
*/
?>

在php中关于在一个函数中定义的变量,包括参数在内都不能访问函数外部变量,并且在默认的情况下,在一个函数外部定义的变量不能访问的函数变量。

下面看实例

 代码如下 复制代码
<?php
$a = 1;
$b = 2;
function Sum()
{
    global $a, $b;
    $b = $a + $b;
}
Sum();
echo $b;
?>

这样返回$b的值为3,在php中global是一个全局变量所有才会这样,那么我们现来看php变量引用实例

 代码如下 复制代码
<?php
function str_unite (&$string)
{
    $string .= '也喜欢蓝色.';
}
$str = '喜欢红色,';
str_unite ($str);
echo $str;    // 输出结果: '喜欢红色,也喜欢蓝色.'
?> 

,上面讲的是函数的作用域的全局变量与函数的引用,下面来看个函数的局部变量

 代码如下 复制代码

<?php
$a = 1;
$b = 2;
function Sum($a,$b)
{
     $b = $a + $b;

     echo $b;//3
}
Sum();//
echo $b;//2
?>

本站原创教程转载注明来源www.111cn.net/phper/php.html

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • PHP函数strip_tags的一个bug浅析

    PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • SQL Server中row_number函数的常见用法示例详解

    这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • PHP加密解密函数详解

    分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
  • php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法

    最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天原因,最后找到了问题所在。 1.使用 PEAR 的...2015-10-21