php下curl与file,phpcurl

本文目录一览:

php中curl爬虫 怎么样通过网页获取所有链接

本文承接上面两篇,本篇中的示例要调用到前两篇中的函数,做一个简单的URL采集。一般php采集网络数据会用file_get_contents、file和cURL。不过据说cURL会比file_get_contents、file更快更专业,更适合采集。今天就试试用cURL来获取网页上的所有链接。示例如下:

?php

/*

* 使用curl 采集hao123.com下的所有链接。

*/

include_once(‘function.php’);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, ”);

// 只需返回HTTP header

curl_setopt($ch, CURLOPT_HEADER, 1);

// 页面内容我们并不需要

// curl_setopt($ch, CURLOPT_NOBODY, 1);

// 返回结果,而不是输出它

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$html = curl_exec($ch);

$info = curl_getinfo($ch);

if ($html === false) {

echo “cURL Error: ” . curl_error($ch);

}

curl_close($ch);

$linkarr = _striplinks($html);

// 主机部分,补全用

$host = ”;

if (is_array($linkarr)) {

foreach ($linkarr as $k = $v) {

$linkresult[$k] = _expandlinks($v, $host);

}

}

printf(“p此页面的所有链接为:/ppre%s/pren”, var_export($linkresult , true));

?

function.php内容如下(即为上两篇中两个函数的合集):

?php

function _striplinks($document) {

preg_match_all(“‘s*as.*?hrefs*=s*([“‘])?(?(1) (.*?)\1 | ([^s]+))’isx”, $document, $links);

// catenate the non-empty matches from the conditional subpattern

while (list($key, $val) = each($links[2])) {

if (!empty($val))

$match[] = $val;

} while (list($key, $val) = each($links[3])) {

if (!empty($val))

$match[] = $val;

}

// return the links

return $match;

}

/*===================================================================*

Function: _expandlinks

Purpose: expand each link into a fully qualified URL

Input: $links the links to qualify

$URI the full URI to get the base from

Output: $expandedLinks the expanded links

*===================================================================*/

function _expandlinks($links,$URI)

{

$URI_PARTS = parse_url($URI);

$host = $URI_PARTS[“host”];

preg_match(“/^[^?]+/”,$URI,$match);

$match = preg_replace(“|/[^/.]+.[^/.]+$|”,””,$match[0]);

$match = preg_replace(“|/$|”,””,$match);

$match_part = parse_url($match);

$match_root =

$match_part[“scheme”].”://”.$match_part[“host”];

$search = array( “|^http://”.preg_quote($host).”|i”,

“|^(/)|i”,

“|^(?!http://)(?!mailto:)|i”,

“|/./|”,

“|/[^/]+/../|”

);

$replace = array( “”,

$match_root.”/”,

$match.”/”,

“/”,

“/”

);

$expandedLinks = preg_replace($search,$replace,$links);

return $expandedLinks;

}

?

php获取数据为什么curl获取不完整?而用file_get_contents能获取完整?

因为,PHP CURL库默认1024字节的长度不等待数据的返回,所以你那段代码需增加一项配置:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Expect:’));

给你一个更全面的封装方法:

function req_curl($url, $status = null, $options = array())

{

    $res = ”;

    $options = array_merge(array(

        ‘follow_local’ = true,

        ‘timeout’ = 30,

        ‘max_redirects’ = 4,

        ‘binary_transfer’ = false,

        ‘include_header’ = false,

        ‘no_body’ = false,

        ‘cookie_location’ = dirname(__FILE__) . ‘/cookie’,

        ‘useragent’ = ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1’,

        ‘post’ = array() ,

        ‘referer’ = null,

        ‘ssl_verifypeer’ = 0,

        ‘ssl_verifyhost’ = 0,

        ‘headers’ = array(

            ‘Expect:’

        ) ,

        ‘auth_name’ = ”,

        ‘auth_pass’ = ”,

        ‘session’ = false

    ) , $options);

    $options[‘url’] = $url;

    $s = curl_init();

    if (!$s) return false;

    curl_setopt($s, CURLOPT_URL, $options[‘url’]);

    curl_setopt($s, CURLOPT_HTTPHEADER, $options[‘headers’]);

    curl_setopt($s, CURLOPT_SSL_VERIFYPEER, $options[‘ssl_verifypeer’]);

    curl_setopt($s, CURLOPT_SSL_VERIFYHOST, $options[‘ssl_verifyhost’]);

    curl_setopt($s, CURLOPT_TIMEOUT, $options[‘timeout’]);

    curl_setopt($s, CURLOPT_MAXREDIRS, $options[‘max_redirects’]);

    curl_setopt($s, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($s, CURLOPT_FOLLOWLOCATION, $options[‘follow_local’]);

    curl_setopt($s, CURLOPT_COOKIEJAR, $options[‘cookie_location’]);

    curl_setopt($s, CURLOPT_COOKIEFILE, $options[‘cookie_location’]);

    if (!empty($options[‘auth_name’])  is_string($options[‘auth_name’]))

    {

        curl_setopt($s, CURLOPT_USERPWD, $options[‘auth_name’] . ‘:’ . $options[‘auth_pass’]);

    }

    if (!empty($options[‘post’]))

    {

        curl_setopt($s, CURLOPT_POST, true);

        curl_setopt($s, CURLOPT_POSTFIELDS, $options[‘post’]);

        //curl_setopt($s, CURLOPT_POSTFIELDS, array(‘username’ = ‘aeon’, ‘password’ = ‘111111’));

    }

    if ($options[‘include_header’])

    {

        curl_setopt($s, CURLOPT_HEADER, true);

    }

    if ($options[‘no_body’])

    {

        curl_setopt($s, CURLOPT_NOBODY, true);

    }

    if ($options[‘session’])

    {

        curl_setopt($s, CURLOPT_COOKIESESSION, true);

        curl_setopt($s, CURLOPT_COOKIE, $options[‘session’]);

    }

    curl_setopt($s, CURLOPT_USERAGENT, $options[‘useragent’]);

    curl_setopt($s, CURLOPT_REFERER, $options[‘referer’]);

    $res = curl_exec($s);

    $status = curl_getinfo($s, CURLINFO_HTTP_CODE);

    curl_close($s);

    return $res;

}

php如何获取通过CURL或file_get_contents抓取者的IP地址

百度搜一下防止采集方面的知识,CURL或file_get_contents可以模拟用户行为,获取ip跟普通用户ip其实是一样的,关键是怎么去区别他们,这就需要在客户端做手脚,一般都用js来在客户端做手脚来区别。

php curl 为什么比file

curl为什么比file_get_contents慢?

还是啥问题?

呃,问太多,我也不懂,反正用就是了,听从大神的测试结果,或者自己不服了,也去跑个测试,实在还想问,那就读源代码~

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/188562.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-28 13:31
下一篇 2024-11-28 13:31

相关推荐

  • PHP和Python哪个好找工作?

    PHP和Python都是非常流行的编程语言,它们被广泛应用于不同领域的开发中。但是,在考虑择业方向的时候,很多人都会有一个问题:PHP和Python哪个好找工作?这篇文章将从多个方…

    编程 2025-04-29
  • PHP怎么接币

    想要在自己的网站或应用中接受比特币等加密货币的支付,就需要对该加密货币拥有一定的了解,并使用对应的API进行开发。本文将从多个方面详细阐述如何使用PHP接受加密货币的支付。 一、环…

    编程 2025-04-29
  • 如何使用yum安装curl、policycoreutils、policycoreutils-python-utils和openssh-server等软件包

    yum是一个常用的包管理器,可以使在Linux系统上安装、更新和卸载软件包变得更加容易。本文将详细介绍使用yum安装curl、policycoreutils、policycoreu…

    编程 2025-04-28
  • 使用PHP foreach遍历有相同属性的值

    本篇文章将介绍如何使用PHP foreach遍历具有相同属性的值,并给出相应的代码示例。 一、基础概念 在讲解如何使用PHP foreach遍历有相同属性的值之前,我们需要先了解几…

    编程 2025-04-28
  • PHP获取301跳转后的地址

    本文将为大家介绍如何使用PHP获取301跳转后的地址。301重定向是什么呢?当我们访问一个网页A,但是它已经被迁移到了另一个地址B,此时若服务器端做了301重定向,那么你的浏览器在…

    编程 2025-04-27
  • PHP登录页面代码实现

    本文将从多个方面详细阐述如何使用PHP编写一个简单的登录页面。 1. PHP登录页面基本架构 在PHP登录页面中,需要包含HTML表单,用户在表单中输入账号密码等信息,提交表单后服…

    编程 2025-04-27
  • Python File文件怎么打开

    Python的File是读写文件的重要操作之一,那么如何打开Python中的文件呢?下面我们从多个方面进行详细的阐述。 一、直接打开文件 可以使用Python的open()函数打开…

    编程 2025-04-27
  • PHP与Python的比较

    本文将会对PHP与Python进行比较和对比分析,包括语法特性、优缺点等方面。帮助读者更好地理解和使用这两种语言。 一、语法特性 PHP语法特性: <?php // 简单的P…

    编程 2025-04-27
  • file*详解

    一、file是什么 在C语言中,我们可以通过定义“file”类型的指针来操作文件,而这个指针所指向的就是文件在内存中的映射。通过对file变量的各种操作,我们可以对文件做读、写、打…

    编程 2025-04-25
  • PHP版本管理工具phpenv详解

    在PHP项目开发过程中,我们可能需要用到不同版本的PHP环境来试验不同的功能或避免不同版本的兼容性问题。或者我们需要在同一台服务器上同时运行多个不同版本的PHP语言。但是每次手动安…

    编程 2025-04-24

发表回复

登录后才能评论