草庐IT

PHP - get_headers 和 SSL 错误

coder 2024-01-03 原文

这是我的代码

$url = 'http://www.wikipedia.com';  // URL WITH HTTP
$hurl = str_replace("http", "https", $url); // URL WITH HTTPS

$urlheads = get_headers($url, 1);   
$surlheads = get_headers($hurl, 1);     
$urlx = false;
$surlx = false;

foreach ($urlheads as $name => $value) 
{
    if ($name === 'Location') 
    {
        $urlx=$value;   
    }
    else{

    }
}
print_r($urlx);

这是我得到的错误:

Warning: get_headers(): Peer certificate CN=`*.wikipedia.org' did not match expected CN=`www.wikipedia.com' in....

Warning: get_headers(): Failed to enable crypto in....

Warning: get_headers(https://www.wikipedia.com): failed to open stream:     operation failed in .....
Array ( [0] => http://www.wikipedia.org/ [1] => https://www.wikipedia.org/ )

为什么会发生这种情况以及从 https 页面获取 header 而没有错误(没有 curl )的正确方法是什么。另外,当我在其他一些 https 站点上尝试时一切正常

最佳答案

问题在于服务器证书被呈现为通配符 * 因此它可以允许同一证书下的所有子域,但由于某些奇怪的原因通配符 * 在导致失败的 SSL 验证期间按字面意义使用。要解决此问题,请使用 stream_context_set_default() 将 SSL 验证设置为 false

stream_context_set_default( [
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
    ],
]);

$url = 'https://www.wikipedia.com';  // URL WITH HTTPS

$headers = get_headers($url, 1);

var_dump($headers);

输出

array(25) {
    [0] => string(30)
    "HTTP/1.1 301 Moved Permanently" ["Date"] => array(2) {
        [0] => string(29)
        "Sun, 27 Nov 2016 15:44:44 GMT" [1] => string(29)
        "Sun, 27 Nov 2016 15:44:44 GMT"
    }["Content-Type"] => array(2) {
        [0] => string(29)
        "text/html; charset=iso-8859-1" [1] => string(9)
        "text/html"
    }["Content-Length"] => array(2) {
        [0] => string(3)
        "234" [1] => string(5)
        "80740"
    }["Connection"] => array(2) {
        [0] => string(5)
        "close" [1] => string(5)
        "close"
    }["Server"] => array(2) {
        [0] => string(18)
        "mw1174.eqiad.wmnet" [1] => string(18)
        "mw1175.eqiad.wmnet"
    }["X-Powered-By"] => array(2) {
        [0] => string(17)
        "HHVM/3.3.0-static" [1] => string(17)
        "HHVM/3.3.0-static"
    }["Location"] => string(26)
    "https://www.wikipedia.org/" ["Cache-Control"] => array(2) {
        [0] => string(15)
        "max-age=2592000" [1] => string(45)
        "s-maxage=86400, must-revalidate, max-age=3600"
    }["Expires"] => string(29)
    "Wed, 21 Dec 2016 14:55:26 GMT" ["Vary"] => array(2) {
        [0] => string(34)
        "X-Forwarded-Proto, Accept-Encoding" [1] => string(15)
        "Accept-Encoding"
    }["X-Varnish"] => array(2) {
        [0] => string(60)
        "252832401 234761536, 189834925 105479673, 503055844 58285403" [1] => string(57)
        "815608054 810788132, 143499750 28230570, 504104889 557059"
    }["Via"] => array(2) {
        [0] => string(46)
        "1.1 varnish-v4, 1.1 varnish-v4, 1.1 varnish-v4" [1] => string(46)
        "1.1 varnish-v4, 1.1 varnish-v4, 1.1 varnish-v4"
    }["Age"] => array(2) {
        [0] => string(6)
        "521357" [1] => string(5)
        "59119"
    }["X-Cache"] => array(2) {
        [0] => string(41)
        "cp1053 hit/4, cp3032 hit/9, cp3030 hit/17" [1] => string(46)
        "cp1054 hit/8, cp3032 hit/33, cp3030 hit/531848"
    }["X-Cache-Status"] => array(2) {
        [0] => string(3)
        "hit" [1] => string(3)
        "hit"
    }["Set-Cookie"] => array(4) {
        [0] => string(88)
        "WMF-Last-Access=27-Nov-2016;Path=/;HttpOnly;secure;Expires=Thu, 29 Dec 2016 12:00:00 GMT" [1] => string(76)
        "GeoIP=GB:WLS:Ammanford:51.79:-3.99:v4; Path=/; secure; Domain=.wikipedia.com" [2] => string(88)
        "WMF-Last-Access=27-Nov-2016;Path=/;HttpOnly;secure;Expires=Thu, 29 Dec 2016 12:00:00 GMT" [3] => string(76)
        "GeoIP=GB:WLS:Ammanford:51.79:-3.99:v4; Path=/; secure; Domain=.wikipedia.org"
    }["X-Analytics"] => array(2) {
        [0] => string(19)
        "https=1;nocookies=1" [1] => string(19)
        "https=1;nocookies=1"
    }["X-Client-IP"] => array(2) {
        [0] => string(13)
        "81.129.193.46" [1] => string(13)
        "81.129.193.46"
    }[1] => string(15)
    "HTTP/1.1 200 OK" ["ETag"] => string(23)
    "W/"
    13 b64 - 541e8 ad5dab71 "" ["Last-Modified"] => string(29)
    "Tue, 22 Nov 2016 19:21:20 GMT" ["Backend-Timing"] => string(24)
    "D=213 t=1479943165198824" ["Strict-Transport-Security"] => string(44)
    "max-age=31536000; includeSubDomains; preload" ["Accept-Ranges"] => string(5)
    "bytes"
}

关于PHP - get_headers 和 SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40830265/

有关PHP - get_headers 和 SSL 错误的更多相关文章

  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. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  4. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  5. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  6. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  7. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  8. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  9. ruby-on-rails - Rails 5 Active Record 记录无效错误 - 2

    我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa

  10. arrays - 这是 Ruby 中 Array.fill 方法的错误吗? - 2

    这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]

随机推荐