欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

Curl 采集乱码与采集不到 PHP,该怎么处理

程序员文章站 2022-03-28 08:09:27
...
Curl 采集乱码与采集不到 PHP
PHP程序是用gbk2312编码的:

$url = "http://www.sina.com.cn";//gbk2312编码
//$url = "http://www.163.com";//gbk2312编码
//$url = "http://www.sohu.com";//gbk2312编码


$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);//返回获取的输出的文本流
$ret = curl_exec($ch);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_close($ch);
echo $ret;

?>

在采集sina.com.cn时,是正常的,但是采集163.com时是为空的,采集sohu.com时是丢码的.
这是怎么回事呢?如何解决?有哪位怎么呀?先谢谢了!!!没多少分了,不好意思。

------解决方案--------------------
别的不说,我就是来拿分的.楼主记得给全分

PHP code


$curl=curl_init('http://www.163.com');
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)');
$html=curl_exec($curl);
var_dump($html);


$curl=curl_init('http://www.sohu.com');
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)');
$html=curl_exec($curl);
//$html=strstr($html,' 0) {   
   switch ($method) {   
     case 8:   
       // Currently the only supported compression method:   
       $data = gzinflate($body);   
       break;   
     default:   
       // Unknown compression method   
       return false;   
   }   
  } else {   
   // I'm not sure if zero-byte body content is allowed.   
   // Allow it for now...  Do nothing...   
  }   
  
  // Verifiy decompressed size and CRC32:   
  // NOTE: This may fail with large data sizes depending on how   
  //      PHP's integer limitations affect strlen() since $isize   
  //      may be negative for large sizes.   
  if ($isize != strlen($data) || crc32($data) != $datacrc) {   
   // Bad format!  Length or CRC doesn't match!   
   return false;   
  }   
  return $data;   
} 
Curl 采集乱码与采集不到 PHP,该怎么处理

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频