初试php中的curl

关于curl的相关函数,可以点击参考这里:

http://www.wapm.cn/phpdoc/zh/ref.curl.html

但试的时候发现google返回的依旧是乱码,而baidu返回的则不是,怀疑可能是提交的时候需要再加一些参数,有空再好好研究一下 :(

curl_setopt 里面详细的参数可以点击这里查看:

http://www.wapm.cn/phpdoc/zh/function.curl-setopt.html

下面的代码只是一个很简单的测试例子,获取请求的状态码、并显示获取的页面。

代码中的注释是另外一种获取网页的方法。更多的例子或项目可以在搜索引擎里搜索到:例如php爬虫、php curl、php抓取网页之类的关键字..

   1: <meta http-equiv='content-type' content='text/html;charset=utf-8' /> 
   2: <?php 
   3:   $url = "http://www.baidu.com/"; 
   4:   // $contents = file_get_contents($url); 
   5:   //   
   6:   // $contents = iconv("gb2312", "utf-8", $contents); 
   7:   // 
   8:   // echo $contents; 
   9:   $ch = curl_init($url); 
  10:   curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);//返回获取的输出的文本流 
  11:   $ret = curl_exec($ch); 
  12:   curl_setopt($ch, CURLOPT_TIMEOUT, 1); 
  13:   //获取请求的http状态      需要放在exec后close之前 
  14:   $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  15:   echo $response_code; 
  16:   curl_close($ch); 
  17:   $ret = mb_convert_encoding($ret, "UTF-8", "gb2312"); 
  18:   echo $ret; 
  19: ?>
原文地址:https://www.cnblogs.com/meteoric_cry/p/1942017.html