草庐IT

php - get_headers() 与 curl () 哪个更快?

coder 2024-04-12 原文

我需要编写一个 php 脚本,它将接受 csv 文件作为输入,然后解析其中提供的产品 url。

然后我需要验证哪些产品 url 存在,哪些不存在。 我有这两个选项 curl()get_headers()

那么你能告诉我哪个更快更可靠吗?

任何帮助将不胜感激。

最佳答案

我用 100 个不同的域测试了这个:

$urls = [
  "http://familyshare.com/",
  "http://elitedaily.com/",
  "http://www.pickthebrain.com/",
  "http://i100.independent.co.uk/",
  "http://thingsorganizedneatly.tumblr.com/",
  "http://www.cheatsheet.com/",
  "https://jet.com/",
  "https://nightwalk.withgoogle.com/en/panorama",
  "http://www.vumble.com/",
  "http://fusion.net/",
  "https://www.zozi.com",
  "http://joshworth.com/dev/pixelspace/pixelspace_solarsystem.html",
  "http://how-old.net/",
  "https://www.dosomething.org",
  "https://devart.withgoogle.com/",
  "http://www.ranker.com/",
  "http://the-toast.net/",
  "https://www.futurelearn.com/",
  "https://croaciaaudio.com/",
  "http://www.thesimpledollar.com/",
  "http://giphy.com/giphytv",
  "http://snapzu.com/",
  "https://www.touchofmodern.com/",
  "http://www.howstuffworks.com/",
  "http://www.sporcle.com/",
  "http://www.factcheck.org/",
  "https://www.privacytools.io/",
  "http://tiffanithiessen.com/",
  "http://www.supercook.com/",
  "http://www.livescience.com/",
  "http://www.freshnessmag.com",
  "http://www.abeautifulmess.com/",
  "http://cardboardboxoffice.com/",
  "http://www.takepart.com/",
  "http://www.fixya.com/",
  "http://bestreviews.com/",
  "http://theodysseyonline.com/",
  "http://justdelete.me/",
  "http://adventure.com/",
  "http://www.carryology.com/",
  "http://whattheysee.tumblr.com/",
  "https://unsplash.com/",
  "http://fromwhereidrone.com/",
  "http://www.attn.com/",
  "http://ourworldindata.org/",
  "http://www.melty.com/",
  "http://www.truthdig.com/",
  "https://tosdr.org/",
  "https://thinga.com/",
  "http://forvo.com/",
  "http://tiii.me/",
  "https://snapguide.com/",
  "http://www.tubefilter.com/",
  "http://www.inherentlyfunny.com/",
  "http://www.someecards.com/",
  "https://this.cm/",
  "http://littlebigdetails.com/",
  "http://clapway.com/",
  "http://www.nerdfitness.com/",
  "http://iwantdis.com/",
  "http://Racked.com",
  "http://thesweetsetup.com/",
  "http://www.we-heart.com/",
  "https://www.revealnews.org/",
  "https://featuredcreature.com/",
  "http://www.scotthyoung.com/blog/",
  "http://www.thehandandeye.com/",
  "http://www.thenorthernpost.com/",
  "http://www.welzoo.com/",
  "http://www.tickld.com/",
  "http://thinksimplenow.com/",
  "http://www.quietrev.com/",
  "http://www.freshoffthegrid.com/",
  "https://www.generosity.com/",
  "http://addicted2success.com/",
  "http://cubiclane.com/",
  "http://waitbutwhy.com/",
  "http://toolsandtoys.net/",
  "http://googling.co/",
  "http://penelopetrunk.com/",
  "http://iaf.tv/",
  "http://artofvisuals.com/",
  "http://www.lifeaftercollege.org/blog",
  "http://listverse.com/",
  "http://chrisguillebeau.com/",
  "http://expeditionportal.com/",
  "http://www.marieforleo.com/",
  "http://mostexclusivewebsite.com/",
  "http://www.alphr.com/",
  "http://www.rtings.com/",
  "http://all-that-is-interesting.com/",
  "http://theunbeatnpath.xyz/",
  "http://www.keepinspiring.me/",
  "https://paidtoexist.com/blog/",
  "http://www.lovethispic.com/",
  "http://riskology.co/blog/",
  "http://geyserofawesome.com/",
  "http://www.eugenewei.com/",
  "http://clickotron.com/"
];


$startTime = microtime(true);
  stream_context_set_default(
      array(
          'http' => array(
              'method' => 'HEAD'
          )
      )
  );
  $headers1 = [];
  foreach ($urls as $url) {
    $headers1[] = get_headers($url);
  }
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
echo "Execution time : $elapsed seconds \n";


$startTime = microtime(true);
  $headers2 = [];
  foreach ($urls as $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,            $url);
    curl_setopt($ch, CURLOPT_HEADER,         true);
    curl_setopt($ch, CURLOPT_NOBODY,         true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT,        10);
    $header2[] = curl_exec($ch);
  }
$endTime = microtime(true);
$elapsed = $endTime - $startTime;
echo "Execution time : $elapsed seconds \n";

get_headers(GET) 与 cURL:

Execution time : 139.95884609222 seconds
Execution time : 65.998840093613 seconds

get_headers(HEAD)与 cURL:

Execution time : 114.60515785217 seconds
Execution time : 66.077962875366 seconds

所以 cURL 确实显着更快。

关于php - get_headers() 与 curl () 哪个更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844926/

有关php - get_headers() 与 curl () 哪个更快?的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  3. ruby - 正则表达式在哪个位置失败? - 2

    我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束

  4. ruby-on-rails - Heroku 吃掉了我的自定义 HTTP header - 2

    我正在使用Heroku(heroku.com)来部署我的Rails应用程序,并且正在构建一个iPhone客户端来与之交互。我的目的是将手机的唯一设备标识符作为HTTPheader传递给应用程序以进行身份​​验证。当我在本地测试时,我的header通过得很好,但在Heroku上它似乎去掉了我的自定义header。我用ruby​​脚本验证:url=URI.parse('http://#{myapp}.heroku.com/')#url=URI.parse('http://localhost:3000/')req=Net::HTTP::Post.new(url.path)#boguspara

  5. ruby-on-rails - 使用 HTTP.get_response 检索 Facebook 访问 token 时出现 Rails EOF 错误 - 2

    我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token

  6. ruby - what is - gets is a directory - 错误信息 - 2

    我遇到了这个奇怪的错误.../Users/gideon/Documents/ca_ruby/rubytactoe/lib/player.rb:13:in`gets':Isadirectory-spec(Errno::EISDIR)player_spec.rb:require_relative'../spec_helper'#theuniverseisvastandinfinite...itcontainsagame....butnoplayersdescribe"tictactoegame"docontext"theplayerclass"doit"musthaveahumanplay

  7. ruby - 使用哪个,eruby 还是 erb? - 2

    eruby和erb有什么区别?哪些考虑因素会促使我选择其中之一?我的应用程序正在为网络设备(路由器、负载平衡器、防火墙等)生成配置文件。我的计划是对配置文件进行模板化,在源文件中使用嵌入式ruby​​(通过eruby或erb)来执行诸如迭代生成路由器的所有接口(interface)配置block之类的操作(这些block都非常相似,仅在标签上有所不同和IP地址)。例如,我可能有这样一个配置模板文件:hostnamesample-routerlogging10.5.16.26当通过嵌入式ruby​​解释器(erb或eruby)运行时,会产生以下输出:hostnamesample-rout

  8. ruby - 如何更快地解决 project euler #21? - 2

    原始问题Letd(n)bedefinedasthesumofproperdivisorsofn(numberslessthannwhichdivideevenlyinton).Ifd(a)=bandd(b)=a,whereab,thenaandbareanamicablepairandeachofaandbarecalledamicablenumbers.Forexample,theproperdivisorsof220are1,2,4,5,10,11,20,22,44,55and110;therefored(220)=284.Theproperdivisorsof284are1,2,

  9. ruby - 在 Ubuntu 14.04 中使用 Curl 安装 RVM 时出错 - 2

    我试图在Ubuntu14.04中使用Curl安装RVM。我运行了以下命令:\curl-sSLhttps://get.rvm.io|bash-sstable出现如下错误:curl:(7)Failedtoconnecttoget.rvm.ioport80:Networkisunreachable非常感谢解决此问题的任何帮助。谢谢 最佳答案 在执行curl之前尝试这个:echoipv4>>~/.curlrc 关于ruby-在Ubuntu14.04中使用Curl安装RVM时出错,我们在Stack

  10. ruby-on-rails - lovdbyless VS 社区引擎……哪个最好? - 2

    随着ruby​​被引入为新的编程救世主,我想知道是否有人基于易用性、运行所需的资源、可用性和易定制性而有偏好。两者有更好的吗? 最佳答案 好吧,任何基于Rails的社交网络应用程序的比较都应该包括insoshi(http://portal.insoshi.com/)。话虽这么说,这三个都非常相似,区别在于实现细节。Lovd和Insoshi都是完整的Rails应用程序;它旨在供您将它们用作入门工具包,并使用您自己的自定义功能对其进行扩展。另一方面,CommunityEngine是一个Rails插件。这意味着您可以更轻松地向现有Rail

随机推荐