doukuizuo1795 2017-09-02 16:04
浏览 54
已采纳

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

I'm trying to get aspx url content b file_get_contents, But when I do this, the information returns inaccurately.

Click to show image

Html:

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

Php code:

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

1条回答 默认 最新

  • doujia1163 2017-09-02 16:32
    关注

    You screenshot look like compressed response. To get plain text, try using this:

    <?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;
    ?>
    

    You can also use below solution but this doesn't work on all servers.

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

    I'll suggest to use cURL code because that does support on all servers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?