0

i have one special issue with file_get_contents in PHP, here is code:

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

it gives me something like this

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

in some pages from this page i can get proper encoding and content with iconv, but here im helpless, how can i fix that? thx

1
  • This answer solves the issue pretty well. Commented Oct 26, 2013 at 3:42

3 Answers 3

2

That page is in UTF-8. You need to set the header to match:

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

Sign up to request clarification or add additional context in comments.

3 Comments

thx, but its not help... and anyway, i dont need to display it, i need to crawl in this code
What exactly are you doing with it? Storing it the DB? Looking for something within it? Did you see: stackoverflow.com/questions/2236668/…
I already tried solutions in that topic, and actualy it works, but not in all pages... for example try that exact page which is in example and yes, i need to save some content to DB - Open Graph data
1

Use cURL. This function is an alternative to 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);

8 Comments

with this i get empty string
can you try it again please? 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;
I edited my post and there is working code. What I noticed in your comment that you don't need ";" after function..
its totaly weird but now this page works... but another "http://www.bratm.sk/viagra-jedine-vysvetlenie-ako-tam-ta-zena-drzi-d-d/" still not, can you try this one too?
if(curl_errno($ch)) { return 'error:' . curl_error($ch); } php.net/manual/en/function.curl-error.php
|
1

I think you're looking for something like this

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.