400-685-0732

WJMonitor舆情之声

企业大数据智能舆情监测管理解决方案

全网监测海量数据按需发布监测预警

实时把握舆情动态精准追溯信息源头

获取验证码
企业采购 个人使用
当前位置: 首页 > SEO博客 > curl,让你成为一个采集高手

curl,让你成为一个采集高手

时间:2014-05-13 10:27:16

类似于dreamhost这类主机服务商,是显示fopen的使用 的。使用php的curl可以实现支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP 上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道就最常用的来说,是基于http的 get和post方法。


代码实现:



1、http的get实现






Php代码  收藏代码



  1. $ch = curl_init("http://www.domain.com/api/index.php?test=1") ;

  2. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回

  3. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回

  4. echo $output = curl_exec($ch) ;

  5. /* 写入文件 */

  6. $fh = fopen("out.html", 'w') ;

  7. fwrite($fh, $output) ;

  8. fclose($fh) ;



2、http的post实现





Php代码  收藏代码



  1. <?php

  2. $url = 'http://www.domain.com/api/' ;

  3. $fields = array(

  4.                'lname'=>'justcoding' ,

  5.                'fname'=>'phplover' ,

  6.                'title'=>'myapi',

  7.                'age'=>'27' ,

  8.                'email'=>'1353777303@gmail.com' ,

  9.                'phone'=>'1353777303'

  10.               );

  11. //$post_data = implode('&',$fields);

  12. //open connection

  13. $ch = curl_init() ;

  14. //set the url, number of POST vars, POST data

  15. curl_setopt($ch, CURLOPT_URL,$url) ;

  16. curl_setopt($ch, CURLOPT_POST,count($fields)) ; // 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。

  17. curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // 在HTTP中的“POST”操作。如果要传送一个文件,需要一个@开头的文件名

  18. ob_start();

  19. curl_exec($ch);

  20. $result = ob_get_contents() ;

  21. ob_end_clean();

  22. echo $result;

  23. //close connection

  24. curl_close($ch) ;



http://www.domain.com/api/index.php





Php代码  收藏代码



  1. <?php

  2. if($_GET['test'])

  3. {

  4.      print_r($_GET);

  5. }

  6. if($_POST)

  7. {

  8.     print_r($_POST);

  9. }




3. php的curl传送cookie






两种方式:

 

一种是自动:


Php代码  收藏代码



  1. curl_setopt($curlHandle, CURLOPT_COOKIEJAR, 'cookie.txt '); //保存

  2. curl_setopt($curlHandle, CURLOPT_COOKIEFILE, 'cookie.txt '); //读取



这样COOKIE会自动跟上去.
不过要分两次,一是先访问产生cookie,接着连结才能用cookie

 

例子:

 


Php代码  收藏代码



  1. <?php

  2. function get_curlcuconent2($filename,$referer)

  3. {

  4.    $cookie_jar = tempnam('./tmp','JSESSIONID');

  5.    $ch = curl_init();

  6.    curl_setopt($ch, CURLOPT_URL, $filename);

  7.    curl_setopt($ch, CURLOPT_HEADER, false);

  8.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  9.    //设置文件读取并提交的cookie路径

  10.    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);

  11.    $filecontent=curl_exec($ch);

  12.    curl_close($ch);

  13.    $ch = curl_init();

  14.    $hostname ="www.domain.com";

  15.    //$referer="http://www.domain.com/";

  16.    curl_setopt($ch, CURLOPT_URL, $filename);

  17.    curl_setopt($ch, CURLOPT_REFERER, $referer); // 看这里,你也可以说你从google来

  18.    curl_setopt($ch, CURLOPT_USERAGENT, "www.domain.com");

  19.    //$request = "JSESSIONID=abc6szw15ozvZ_PU9b-8r"; //设置POST参数

  20.    //curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

  21.    // 上面这句,当然你可以说你是baidu,改掉这里的值就ok了,可以实现小偷的功能,$_SERVER['HTTP_USER_AGENT']

  22.    //你也可以自己做个 spider 了,那么就伪装这里的 CURLOPT_USERAGENT 吧

  23.    //如果你要把这个程序放到linux上用php -q执行那也要写出具体的$_SERVER['HTTP_USER_AGENT'],伪造的也可以

  24.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  25.    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);

  26.    curl_setopt($ch, CURLOPT_HEADER, false);//设定是否输出页面内容

  27.    curl_setopt($ch, CURLOPT_GET, 1); // post,get 过去

  28.    $filecontent = curl_exec($ch);

  29.    preg_match_all("/charset=(.+?)[NULL\"\']/is",$filecontent, $charsetarray);

  30.    if(strtolower($charsetarray[1][0])=="utf-8")

  31.          $filecontent=iconv( 'utf-8', 'gb18030//IGNORE' , $filecontent);

  32.    curl_close($ch);

  33.    return $filecontent;

  34. }

  35. ?>



一种自定义:








Php代码  收藏代码



  1. $header[]= 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, * '. '/* ';

  2. $header[]= 'Accept-Language: zh-cn ';

  3. $header[]= 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ';

  4. $header[]= 'Host: '.$你的目标HOST;

  5. $header[]= 'Connection: Keep-Alive ';

  6. $header[]= 'Cookie: '.$你的COOKIE串;

  7. curl_setopt($curlHandel,CURLOPT_HTTPHEADER,$header);


分享按钮