PHP curl_getinfo($ch, CURLINFO_CONTENT_TYPE) retur

2019-08-20 02:44发布

I have the following PHP code:

<?php
  # URL #1
  $ch = curl_init('http://www.google.com/');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_exec($ch);
  # get the content type
  $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  echo $content_type;
  echo "<br>";

  # URL #2
  $ch = curl_init('http://www.lemonde.fr/');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_exec($ch);
  # get the content type
  $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  echo $content_type;

?>

Returns following result:

text/html charset=UTF-8

text/html

So I am missing the charset for the second URL. I checked the html code and the charset is there.

Why is the curl_getinfo not getting the charset in the second case?

1条回答
Anthone
2楼-- · 2019-08-20 03:25

This information is not sent in the HTML code, but in the HTTP headers. If the curl_getinfo call does not return it, the server did not send it in its HTTP headers.

查看更多
登录 后发表回答