【问题标题】:get_meta_tags() throwing error failed to open stream: HTTP request failed! HTTP/1.1 403 Forbiddenget_meta_tags() 抛出错误未能打开流:HTTP 请求失败! HTTP/1.1 403 禁止
【发布时间】:2019-10-02 10:04:53
【问题描述】:

我正在尝试使用 get_meta_tags() 函数从网站 URL 获取元数据。我插入的大多数 URL 都可以正常工作,但有 1 个 URL 抛出错误 failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

我想知道是否有办法在获得许可的情况下通过?如果不能,有什么方法可以检测特定网站是否可以访问?至少我可以做一些事情来解决它而不会出现错误,因为我需要从元数据中获取一些信息。

我的代码就是这样写的:

get_meta_tags("https://www.udemy.com/course/beginning-c-plus-plus-programming/");

【问题讨论】:

  • 不,如果访问被关闭,您将无法通过。执行file_get_contents('https://www.udemy.com/course/beginning-c-plus-plus-programming/') 时是否遇到同样的错误?
  • @freeek 是的,我遇到了同样的错误。

标签: php get-meta-tags


【解决方案1】:

看起来网站阻止了 PHP 脚本以防止抓取。

您可以尝试让网站认为它是由人类(网络浏览器)访问的。

您可以在请求期间使用stream_context_create() 更改User-Agent 标头:

$context = stream_context_create(
    array(
        "http" => array(
            "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
        )
    )
);

$tags = get_meta_tags(file_get_contents('https://www.udemy.com/course/beginning-c-plus-plus-programming/', false, $context));
var_dump($tags)

Here你可以找到最常用的用户代理列表。

附:请记住,这并不真正公平

【讨论】:

    【解决方案2】:

    你可以使用 cURL

    function url_get_contents($url, $useragent='cURL', $headers=false, $follow_redirects=true, $debug=false) {
    
        // initialise the CURL library
        $ch = curl_init();
    
        // specify the URL to be retrieved
        curl_setopt($ch, CURLOPT_URL,$url);
    
        // we want to get the contents of the URL and store it in a variable
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    
        // specify the useragent: this is a required courtesy to site owners
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    
        // ignore SSL errors
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
        // return headers as requested
        if ($headers==true){
            curl_setopt($ch, CURLOPT_HEADER,1);
        }
    
        // only return headers
        if ($headers=='headers only') {
            curl_setopt($ch, CURLOPT_NOBODY ,1);
        }
    
        // follow redirects - note this is disabled by default in most PHP installs from 4.4.4 up
        if ($follow_redirects==true) {
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
        }
    
        // if debugging, return an array with CURL's debug info and the URL contents
        if ($debug==true) {
            $result['contents']=curl_exec($ch);
            $result['info']=curl_getinfo($ch);
        }
    
        // otherwise just return the contents as a variable
        else $result=curl_exec($ch);
    
        // free resources
        curl_close($ch);
    
        // send back the data
        return $result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 2012-06-23
      • 1970-01-01
      相关资源
      最近更新 更多