<i id='NDMDk'><tr id='NDMDk'><dt id='NDMDk'><q id='NDMDk'><span id='NDMDk'><b id='NDMDk'><form id='NDMDk'><ins id='NDMDk'></ins><ul id='NDMDk'></ul><sub id='NDMDk'></sub></form><legend id='NDMDk'></legend><bdo id='NDMDk'><pre id='NDMDk'><center id='NDMDk'></center></pre></bdo></b><th id='NDMDk'></th></span></q></dt></tr></i><div id='NDMDk'><tfoot id='NDMDk'></tfoot><dl id='NDMDk'><fieldset id='NDMDk'></fieldset></dl></div>
        <bdo id='NDMDk'></bdo><ul id='NDMDk'></ul>

      <small id='NDMDk'></small><noframes id='NDMDk'>

      <legend id='NDMDk'><style id='NDMDk'><dir id='NDMDk'><q id='NDMDk'></q></dir></style></legend>

      <tfoot id='NDMDk'></tfoot>

        file_get_contents() 给我 403 Forbidden

        时间:2024-08-22
            • <i id='qsIGO'><tr id='qsIGO'><dt id='qsIGO'><q id='qsIGO'><span id='qsIGO'><b id='qsIGO'><form id='qsIGO'><ins id='qsIGO'></ins><ul id='qsIGO'></ul><sub id='qsIGO'></sub></form><legend id='qsIGO'></legend><bdo id='qsIGO'><pre id='qsIGO'><center id='qsIGO'></center></pre></bdo></b><th id='qsIGO'></th></span></q></dt></tr></i><div id='qsIGO'><tfoot id='qsIGO'></tfoot><dl id='qsIGO'><fieldset id='qsIGO'></fieldset></dl></div>
                <tbody id='qsIGO'></tbody>

              <small id='qsIGO'></small><noframes id='qsIGO'>

              <tfoot id='qsIGO'></tfoot>

                  <bdo id='qsIGO'></bdo><ul id='qsIGO'></ul>

                • <legend id='qsIGO'><style id='qsIGO'><dir id='qsIGO'><q id='qsIGO'></q></dir></style></legend>
                  本文介绍了file_get_contents() 给我 403 Forbidden的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个合作伙伴创建了一些内容供我抓取.
                  我可以使用浏览器访问该页面,但是在尝试使用 file_get_contents 时,我得到了 403 禁止.

                  I have a partner that has created some content for me to scrape.
                  I can access the page with my browser, but when trying to user file_get_contents, I get a 403 forbidden.

                  我尝试过使用 stream_context_create,但这并没有帮助 - 可能是因为我不知道应该在那里输入什么.

                  I've tried using stream_context_create, but that's not helping - it might be because I don't know what should go in there.

                  1) 我有什么方法可以抓取数据吗?
                  2) 如果没有,并且不允许合作伙伴配置服务器允许我访问,我该怎么办?

                  1) Is there any way for me to scrape the data?
                  2) If no, and if partner is not allowed to configure server to allow me access, what can I do then?

                  我尝试使用的代码:

                  $opts = array(
                    'http'=>array(
                      'user_agent' => 'My company name',
                      'method'=>"GET",
                      'header'=> implode("
                  ", array(
                        'Content-type: text/plain;'
                      ))
                    )
                  );
                  
                  $context = stream_context_create($opts);
                  
                  //Get header content
                  $_header = file_get_contents($partner_url,false, $context);
                  

                  推荐答案

                  这不是您脚本中的问题,它是您合作伙伴 Web 服务器安全性中的一项功能.

                  This is not a problem in your script, its a feature in you partners web server security.

                  很难说到底是什么阻碍了你,很可能是某种阻碍抓取.如果您的合作伙伴可以访问他的网络服务器设置,这可能有助于查明.

                  It's hard to say exactly whats blocking you, most likely its some sort of block against scraping. If your partner has access to his web servers setup it might help pinpoint.

                  您可以做的是通过设置用户代理标头来伪造网络浏览器",使其模仿标准网络浏览器.

                  What you could do is to "fake a web browser" by setting the user-agent headers so that it imitates a standard web browser.

                  我建议使用 cURL 来执行此操作,并且很容易找到执行此操作的好文档.

                  I would recommend cURL to do this, and it will be easy to find good documentation for doing this.

                      // create curl resource
                      $ch = curl_init();
                  
                      // set url
                      curl_setopt($ch, CURLOPT_URL, "example.com");
                  
                      //return the transfer as a string
                      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                      curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
                  
                      // $output contains the output string
                      $output = curl_exec($ch);
                  
                      // close curl resource to free up system resources
                      curl_close($ch); 
                  

                  这篇关于file_get_contents() 给我 403 Forbidden的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:检查是否从 iOS 设备访问 PHP 页面 下一篇:为什么我将 mime 类型的 .csv 文件作为“application/octet-stream"?

                  相关文章

                • <tfoot id='i8xSB'></tfoot>

                    • <bdo id='i8xSB'></bdo><ul id='i8xSB'></ul>

                    1. <i id='i8xSB'><tr id='i8xSB'><dt id='i8xSB'><q id='i8xSB'><span id='i8xSB'><b id='i8xSB'><form id='i8xSB'><ins id='i8xSB'></ins><ul id='i8xSB'></ul><sub id='i8xSB'></sub></form><legend id='i8xSB'></legend><bdo id='i8xSB'><pre id='i8xSB'><center id='i8xSB'></center></pre></bdo></b><th id='i8xSB'></th></span></q></dt></tr></i><div id='i8xSB'><tfoot id='i8xSB'></tfoot><dl id='i8xSB'><fieldset id='i8xSB'></fieldset></dl></div>

                      <small id='i8xSB'></small><noframes id='i8xSB'>

                      <legend id='i8xSB'><style id='i8xSB'><dir id='i8xSB'><q id='i8xSB'></q></dir></style></legend>