【问题标题】:Warning: get_headers(): SSL operation failed with code 1 on online free subdomaine, solution?警告:get_headers(): SSL operation failed with code 1 on online free subdomaine,解决方案?
【发布时间】:2021-05-21 12:27:12
【问题描述】:

感谢您花时间尝试和帮助,我真的很感激。 让我先解释一下我在做什么,我试图通过阅读它的元标签从新闻网站获取一张图片。所以当我在我的本地主机上运行它时没有错误,但是当我在我的免费在线子域上上传脚本时,就会出现错误,并且一些链接不是我尝试的,而是一些链接。这是错误:

警告:get_headers():无法在第 81 行的 /home/vol15_7/byethost7.com/b7_27905999/htdocs/cron/news_cron_1.php 中启用加密

警告:get_headers(http://rss.cnn.com/~r/rss/edition_world/~3/6uMCnNwt5F4/index.html):打开流失败:/home/vol15_7/byethost7.com/b7_27905999/htdocs/cron/news_cron_1.php 第 81 行中的操作失败

第 81 行是:

$url_redirect = get_headers($link);

我尝试了这个解决方案,但没有奏效:

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

我需要你的帮助,这是我的代码:

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

            $url_redirect = get_headers($link);
            //var_dump($url_redirect);
            $location_redirect = $url_redirect[0];

            if (strpos($location_redirect, " 301 ") !== false){
                $redirect_array = explode("Location: ", $url_redirect[1]);
                $location_url = $redirect_array[1];
            } else {
                $location_url = $link;
            }

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $location_url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

            $result = curl_exec($curl);
            curl_close($curl);

            $htmlentities = htmlentities($result);

            if($htmlentities != ""){
                // if htmlentites is not empty we make the rest
                $htmlentities = htmlentities($result);

                libxml_use_internal_errors(true);
                $htmlDom = new DOMDocument();
                $htmlDom->loadHTML($htmlentities);
                //var_dump($htmlDom);
                libxml_clear_errors();
                $htmlstring = $htmlDom->textContent;
                /// code continues from here to grab the image from meta tags ----------------------------
            }

【问题讨论】:

  • 你研究过错误信息了吗? stackoverflow.com/questions/14078182/…
  • 这段代码有点奇怪 - 你使用get_headers 来确定链接是否有重定向,但是你将结果传递给 Curl 并启用了 FOLLOW_LOCATION。您应该能够将 $link 直接传递给 Curl 代码,并且它会有额外的好处,它根本不需要引发错误的行。
  • @iainn 的重点是某些网站有 301 错误,所以我试图找到一个解决方案,将帖子的重定向链接作为字符串到目前为止这是我的解决方案,如果你有更好的一个我真的很感激:)
  • “关键是某些网站有 301 错误” - 301 不是错误。请正确解释您要在这里解决的实际问题,因为这仍然很不清楚。我不明白 not 只是让 cURL 自动跟随重定向,这与您声明的意图有关,从元标记中获取图像 URL。如果您遵循任何 HTTP 重定向,那么在这种情况下您想读取哪些元标记?
  • “我不会让 curl 重定向,因为它会自动打开我不想要的网站,但我想静默访问它的 html 元素” - 不知道你想说什么,这没有任何意义。 cURL 发出 HTTP 请求,就是这样。不确定您在此处所说的“打开网站”指的是什么。

标签: php http-headers


【解决方案1】:

所以解决方案是@iainn 的建议,到目前为止似乎有效,所以感谢@iainn。

这就是我所做的:

            $curl = curl_init();
            // I placed the original $link
            curl_setopt($curl, CURLOPT_URL, $link);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);

            $result = curl_exec($curl);
            curl_close($curl);

            $htmlentities = htmlentities($result);

【讨论】:

    猜你喜欢
    • 2020-01-03
    • 2018-01-28
    • 2013-04-30
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多