CURL返回垃圾
问题描述:
我想刮538棒球赔率网站。CURL返回垃圾
当我将URL粘贴到Chrome并查看源代码时,它看起来像标准HTML。
当我凑的数据(我用下面两个密码和file_get_contents
具有相同的结果),我得到的东西看起来像: } KS8> j [ ߔ8
我试过了简单网站上的代码,没有问题。该网站以某种方式阻止我的获得?
<?php
function curl($url) {
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}
$output = curl('https://projects.fivethirtyeight.com/2017-mlb-predictions/games/');
echo $output;
?>
答
config CURLOPT_ENCODING
对于卷曲,那么它会好的。
<?php
function curl($url) {
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
curl_setopt($ch, CURLOPT_ENCODING ,"");
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}
$output = curl('https://projects.fivethirtyeight.com/2017-mlb-predictions/games/');
echo $output;
?>
+1
感谢。我的工作是增加:curl_setopt($ ch,CURLOPT_ENCODING,“gzip”); – shmb6508
使用本也卷曲'curl_setopt($ CH,CURLOPT_ENCODING, '身份');' – Gaurav