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/zh-tw/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

發表回復

登錄後才能評論