如何在file_get_contents中正确解码aspx url?

时间:2017-09-02 16:04:45

标签: php html unicode

我试图获取aspx url内容b file_get_contents,但是当我这样做时,信息返回不准确。

Click to show image

HTML:

<head>
  <meta charset="UTF-8">
</head>

Php代码:

<?php
    $url = "http://www.tsetmc.com/tsev2/data/instinfofast.aspx?i=65004959184388996&c=27+";
    $data = file_get_contents($url);
    echo $data;
?>

1 个答案:

答案 0 :(得分:1)

您的屏幕截图看起来像压缩响应。要获得纯文本,请尝试使用:

<?php
    $url = "http://www.tsetmc.com/tsev2/data/instinfofast.aspx?i=65004959184388996&c=27+";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    curl_close($curl);
    echo $data;
?>

您也可以使用以下解决方案,但这不适用于所有服务器。

$context = stream_context_create(array(
    'http'=>array(
        'method' => "GET",
        'header' => "Accept-Encoding: gzip;q=0, compress;q=0\r\n",
    )
));
$url = "http://www.tsetmc.com/tsev2/data/instinfofast.aspx?i=65004959184388996&c=27+";
$data  = file_get_contents($url, false, $contenxt);
echo $data;

我建议使用cURL代码,因为支持所有服务器。

相关问题