首页 > 解决方案 > PHP cURL 返回加密的 html 页面

问题描述

我正在尝试从 PHP 上的 cURL GET-request 获取简单的 html 代码。
url 上的默认 get-request,例如http://example.com/(不完全是这个域),返回我需要的 html 代码,但是这个域的页面上的 get-request,例如http://example.com/something返回gzip 加密数据,什么的。
我已经尝试解决此问题:

curl_setopt(ch, CURLOPT_ENCODING, ''); // returns ''
curl_setopt(ch, CURLOPT_ENCODING, 'gzip'); // returns ''
curl_setopt(ch, CURLOPT_ENCODING, 'gzip,compressed'); // returns ''
$html = gzdecode($data); // data error

顺便说一句,在检查器上,如Fiddler,此页面返回类似的奇怪符号,但只需单击一下即可修复:“单击解密”。如何使用 PHP 以编程方式解密我的数据?

标签: phpurlcurlrequestphp-curl

解决方案


如果我理解你的话,你需要从一个 url 获取 HTML 中的内容。

请检查这个链接: Get HTML using curl in PHP

您不需要在 curl_setopt 中使用 CURLOPT_ENCODING。

编辑

我试过这个并且它有效:

<?php
function get_data($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$html_content = get_data('https://stackoverflow.com/questions/61548866/php-curl-returns-encrypted-html-page/61549219?noredirect=1#comment108875034_61549219');

echo "You are getting HTML code from an url <br>".$html_content;
?>

在 localhost 中进行测试的图像

谢谢,希望对你有帮助。


推荐阅读