【问题标题】:PHP file_get_contents specific encodingPHP file_get_contents 特定编码
【发布时间】:2013-02-24 15:35:53
【问题描述】:

我对 PHP 中的 file_get_contents 有一个特殊问题,代码如下:

$html = file_get_contents("http://www.bratm.sk/trochu-harlem-inspiracie-zaslite-nam-svoj-super-harlem-shake-ak-bude-husty-zverejnime-ho");

它给了我这样的东西

í}KsŰĆŇčÚŽň!Ç& zRŚ|iI[#)öIkI ĆĺăĹŮÝĹÍý_ŐŃâ[EVßîV%Ů˙ëvĎ ř)śG#óčééééÇĚ çáÜöÁÖÉ;¤áˇ,rřĂăçť[DQ5íeaKÓśOśÉ?ě='šL¸ÔöLßä6ľ4mg_!JĂ÷˘Śu:L§án];9ŇÎV+ި1|C

在此页面的某些页面中,我可以使用 iconv 获得正确的编码和内容,但是我无能为力,我该如何解决?谢谢

【问题讨论】:

标签: php encoding utf-8 character-encoding file-get-contents


【解决方案1】:

该页面采用 UTF-8 格式。您需要设置标题以匹配:

header('Content-Type: text/html; charset=utf-8');

【讨论】:

  • thx,但它无济于事......无论如何,我不需要显示它,我需要在这段代码中爬行
  • 你到底在用它做什么?将其存储在数据库中?在里面寻找什么?你看到了吗:stackoverflow.com/questions/2236668/…
  • 我已经尝试过该主题的解决方案,实际上它可以工作,但并非在所有页面中...例如尝试示例中的确切页面,是的,我需要将一些内容保存到数据库- 打开图表数据
【解决方案2】:

使用 cURL。此函数是 file_get_contents 的替代函数。

function url_get_contents($Url) {
if (!function_exists('curl_init')){
    die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$data = url_get_contents("http://www.bratm.sk/trochu-harlem-inspiracie-zaslite-nam-svoj-super-harlem-shake-ak-bude-husty-zverejnime-ho/");
print_r($data);

【讨论】:

  • 有了这个我得到空字符串
  • 你能再试一次吗? function url_get_contents ($Url) { if (!function_exists('curl_init')){ die('CURL is not installed!'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $Url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); return $output; }; $html = url_get_contents("http://www.bratm.sk/trochu-harlem-inspiracie-zaslite-nam-svoj-super-harlem-shake-ak-bude-husty-zverejnime-ho/"); echo $html;exit;
  • 我编辑了我的帖子并且有工作代码。我在您的评论中注意到您不需要“;”函数后..
  • 它很奇怪,但现在这个页面可以工作了......但是另一个"http://www.bratm.sk/viagra-jedine-vysvetlenie-ako-tam-ta-zena-drzi-d-d/"仍然没有,你也可以试试这个吗?
  • if(curl_errno($ch)) { return 'error:' 。 curl_error($ch); } php.net/manual/en/function.curl-error.php
【解决方案3】:

我认为您正在寻找类似的东西

$opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$context = stream_context_create($opts);

$filename = "http://www.bratm.sk/trochu-harlem-inspiracie-zaslite-nam-svoj-super-harlem-    shake-ak-bude-husty-zverejnime-ho";
echo file_get_contents($filename, false, $context);

【讨论】:

    猜你喜欢
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多