我想从PHP的cURL GET-request中获取简单的html代码。默认的get-request在url上,比如 http:/example.com (不完全是这个域名),返回我所需要的html代码,但在这个域名的页面上获取请求,如 http:/example.comsomething 返回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
顺便说一下,在检查器上,像 提琴手,此页面返回类似的诡异符号,但它通过一键修复。'点击解密'. 我如何用PHP来解密我的数据?
如果我理解得很好,你需要从一个url中获取HTML的内容。
请检查这个链接。在PHP中使用curl从URL中获取HTML
你不需要在 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;
?>
Image with test working in localhost
谢谢你,希望对你有所帮助。