search
HomeBackend DevelopmentPHP TutorialHow to scan url for dead links using php

这篇文章主要介绍了关于使用php如何扫描url死链接,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

* 从Packagist上搜索需要的包

  https://packagist.org/

* 通过composer下载依赖包

composer require guzzlehttp/guzzle
composer require league/csv

* 使用composer自动加载器, 编写scan.php

<?php

// 1. 使用composer自动加载器
require &#39;vendor/autoload.php&#39;;

use GuzzleHttp\RequestOptions;

// 2. 实例Guzzle HTTP客户端
$client = new \GuzzleHttp\Client();

$options = [
    RequestOptions::TIMEOUT => 3,
    RequestOptions::DECODE_CONTENT => false,
    RequestOptions::HEADERS => [
        &#39;User-Agent&#39; => &#39;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36&#39;,
    ]
];

// 3. 打开迭代处理csv
// 或者传用户自定义命令行参数指定输入文件 $argv[1]
// $file = new SplFileObject(&#39;../data/t_video.csv&#39;); /* $csvRow[4], $csvRow[5] */
$file = new SplFileObject(&#39;../data/urls.csv&#39;);
$csv = \League\Csv\Reader::createFromFileObject($file);
foreach ($csv as $csvRow) {
    $url = $csvRow[0];
    echo &#39;scanning &#39;,$url,&#39;... &#39;;
    try {
        // 4. 发送http options请求
        $httpResponse = $client->request(&#39;GET&#39;, $url, $options);

        // 5. 检查http相应的状态码
        $code = $httpResponse->getStatusCode();
        if ($code === 200) {
            echo "\033[32m[OK]\033[0m",PHP_EOL;
        } else {
            throw new \Exception();
        }

    } catch (\Exception $e) {
        // 6. 把死链发给标准输出
        // echo $url.PHP_EOL;
        echo "\033[31m[ERROR]\033[0m ".$e->getMessage().PHP_EOL;
    }
}

* input csv:

../data/urls.csv

https://www.baidu.com
https://mail.qq.com/cgi-bin/frame_html?sid=CYcBjsDbOqznWhVO&r=375cccc57697ed7d00ae5d751663a71c
https://pan.baidu.com/disk/home?errno=0&errmsg=Auth%20Login%20Sucess&&bduss=&ssnerror=0&traceid=#/all?vmode=list&path=%2F05.php%2F25K%20PHP%E9%9D%A2%E8%AF%95%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B
http://dict.youdao.com/w/eng/components/#keyfrom=dict2.index
http://php.net/manual/en/splfileobject.fwrite.php
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=ansi%20%E7%BB%88%E7%AB%AF%E9%A2%9C%E8%89%B2%20%5B%5C33&rsv_spt=1&oq=ansi%2520%25E7%25BB%2588%25E7%25AB%25AF%25E9%25A2%259C%25E8%2589%25B2&rsv_pq=8b17bd6e0027882b&rsv_t=fcf6oR2SbHi9Cpu2eThdv3AQvGwSDf7ecjv7QBvjXoZ3SMpBem3pdNzlNRNmuOW%2BEowe&rqlang=cn&rsv_enter=1&inputT=2640&rsv_sug3=68&rsv_sug2=0&rsv_sug4=3243
https://blog.csdn.net/SLASH_24/article/details/54846392
https://www.jb51.net/article/42358.htm
https://www.cnblogs.com/xudong-bupt/p/3721210.html
http://www.cnblogs.com/mingzhanghui/p/9314906.html
https://packagist.org/packages/maatwebsite/excel
https://www.phptherightway.com/#use_the_current_stable_version
https://doc.phpspider.org/methods.html
http://nosuchurl
http://deadurl

output:

 * 在Linux终端输出带颜色的文字的方法

  注意 echo "" 要用双引号, 单引号会原样输出 \033[32mxxx\033[0m

一、shell下的实现方法

只要设置输出属性,就可输出带颜色的文字 ,shell中的部分属性:

      \033[0m 关闭所有属性
      \033[1m 设置高亮度
      \033[4m 下划线
      \033[5m 闪烁
      \033[7m 反显
      \033[8m 消隐
      \033[30m 至 \33[37m 设置前景色
      \033[40m 至 \33[47m 设置背景色
      \033[nA 光标上移n行 
      \033[nB 光标下移n行
      \033[nC 光标右移n行
      \033[nD 光标左移n行
      \033[y;xH设置光标位置
      \033[2J 清屏
      \033[K 清除从光标到行尾的内容
      \033[s 保存光标位置 
      \033[u 恢复光标位置
      \033[?25l 隐藏光标
      \033[?25h 显示光标

      各数字所代表的颜色如下:
      字背景颜色范围:40----49
      40:黑
      41:深红
      42:绿
      43:黄色
      44:蓝色
      45:紫色
      46:深绿
      47:白色

  字颜色:30----39
      30:黑
      31:红
      32:绿
      33:黄
      34:蓝色
      35:紫色
      36:深绿 
      37:白色

      使用,如:echo -e "\033[34mHello, world!" (-e作用是引导设置输出属性),

      恢复属性为默认值:echo -e  "\033[0m",

      同类的多种设置项可以组合在一起,中间用分号(;)隔开。如下:

      echo -e "\033[20;1H\033[1;4;34mHello,world\033[0m"

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

 如何解决PHP的高并发和大流量的问题

PHP生成带参数的微信小程序的二维码

The above is the detailed content of How to scan url for dead links using php. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor