1
$str = "https://www.google.com/search?q=sd";
echo file_get_contents($str);

I'm try to load page from google with the function file_get_contents, but the output of chracters of utf-8 showing like question mark.

I've tried all the possibilities introduced here: file_get_contents() converts UTF-8 to ISO-8859-1 But the problem is still not resolved.

I would appreciate any help very.

UPDATE:

I see that the problem exists through Google, other sites content is displayed correctly.

enter image description here

Community
  • 1
  • 1
ABE
  • 734
  • 2
  • 9
  • 21

2 Answers2

2

[php]

//charset.php?case=1
//charset.php?case=2
//charset.php?case=3

$case = isset($_GET['case']) ? $_GET['case'] : 1;

if( !in_array($case,range(1,3)) ) $case = 1;


if( $case==1 ) {
    header("Content-type: text/html; charset=tis-620"); //http://htmlpurifier.org/docs/enduser-utf8.html
    $str = "https://www.google.co.th/search?q=sd";
}

if( $case==2 ) {
    header("Content-type: text/html; charset=ISO-8859-1");
    $str = "https://www.google.de/search?q=sd";
}   

if( $case==3 ) {
    header("Content-type: text/html; charset=ISO-8859-9");
    $str = "https://www.google.com.tr/search?q=sd";
}


$data = file_get_contents($str);
echo $data;

[/php]

as you can see ... the correct charset in php header is the solution

CatalinB
  • 571
  • 4
  • 11
1

try once this code it worked for me ..

    <?php 
$abc = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$some_context = stream_context_create($abc);
$filename = "https://www.google.com/search?q=sd";
echo file_get_contents($filename, false, $some_context);
    ?>
Vivek Singh
  • 2,453
  • 1
  • 14
  • 27