PHP的curl实现get,post 和 cookie(几个实例)

http://justcoding.iteye.com/blog/842371

 

类似于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. /* 写入文件 */  
  7. $fh = fopen("out.html"'w') ;   
  8. fwrite($fh$output) ;   
  9. fclose($fh) ;   
$ch = curl_init("http://www.domain.com/api/index.php?test=1") ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
echo $output = curl_exec($ch) ;

/* 写入文件 */
$fh = fopen("out.html", 'w') ;
fwrite($fh, $output) ;
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.   
  13. //open connection   
  14. $ch = curl_init() ;   
  15. //set the url, number of POST vars, POST data   
  16. curl_setopt($ch, CURLOPT_URL,$url) ;   
  17. curl_setopt($ch, CURLOPT_POST,count($fields)) ; // 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。   
  18. curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // 在HTTP中的“POST”操作。如果要传送一个文件,需要一个@开头的文件名   
  19.   
  20. ob_start();   
  21. curl_exec($ch);   
  22. $result = ob_get_contents() ;   
  23. ob_end_clean();   
  24.   
  25. echo $result;   
  26.   
  27. //close connection   
  28. curl_close($ch) ;  
<?php
$url = 'http://www.domain.com/api/' ;
$fields = array(
               'lname'=>'justcoding' ,
               'fname'=>'phplover' ,
               'title'=>'myapi',
               'age'=>'27' ,
               'email'=>'1353777303@gmail.com' ,
               'phone'=>'1353777303'
              );
//$post_data = implode('&',$fields);

//open connection
$ch = curl_init() ;
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL,$url) ;
curl_setopt($ch, CURLOPT_POST,count($fields)) ; // 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); // 在HTTP中的“POST”操作。如果要传送一个文件,需要一个@开头的文件名

ob_start();
curl_exec($ch);
$result = ob_get_contents() ;
ob_end_clean();

echo $result;

//close connection
curl_close($ch) ;
 

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

 

Php代码 复制代码  收藏代码
  1. <?php   
  2.   
  3. if($_GET['test'])   
  4. {   
  5.      print_r($_GET);   
  6. }   
  7.   
  8. if($_POST)   
  9. {   
  10.     print_r($_POST);   
  11. }   
<?php

if($_GET['test'])
{
	 print_r($_GET);
}

if($_POST)
{
	print_r($_POST);
} 

 

3. php的curl传送cookie

 

两种方式:

 

一种是自动:

 

Php代码 复制代码  收藏代码
  1. curl_setopt($curlHandle, CURLOPT_COOKIEJAR, 'cookie.txt '); //保存   
  2. curl_setopt($curlHandle, CURLOPT_COOKIEFILE, 'cookie.txt '); //读取  
curl_setopt($curlHandle, CURLOPT_COOKIEJAR, 'cookie.txt '); //保存
curl_setopt($curlHandle, CURLOPT_COOKIEFILE, 'cookie.txt '); //读取
 

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

 

例子:

 

Php代码 复制代码  收藏代码
  1. <?php      
  2.   
  3. function get_curlcuconent2($filename,$referer)   
  4. {   
  5.    $cookie_jar = tempnam('./tmp','JSESSIONID');   
  6.           
  7.    $ch = curl_init();   
  8.    curl_setopt($ch, CURLOPT_URL, $filename);   
  9.    curl_setopt($ch, CURLOPT_HEADER, false);   
  10.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
  11.   
  12.    //设置文件读取并提交的cookie路径   
  13.    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);   
  14.    $filecontent=curl_exec($ch);   
  15.    curl_close($ch);   
  16.           
  17.    $ch = curl_init();   
  18.    $hostname ="www.domain.com";   
  19.    //$referer="http://www.domain.com/";   
  20.    curl_setopt($ch, CURLOPT_URL, $filename);   
  21.    curl_setopt($ch, CURLOPT_REFERER, $referer); // 看这里,你也可以说你从google来   
  22.    curl_setopt($ch, CURLOPT_USERAGENT, "www.domain.com");   
  23.   
  24.    //$request = "JSESSIONID=abc6szw15ozvZ_PU9b-8r"; //设置POST参数   
  25.    //curl_setopt($ch, CURLOPT_POSTFIELDS, $request);      
  26.    // 上面这句,当然你可以说你是baidu,改掉这里的值就ok了,可以实现小偷的功能,$_SERVER['HTTP_USER_AGENT']   
  27.    //你也可以自己做个 spider 了,那么就伪装这里的 CURLOPT_USERAGENT 吧   
  28.    //如果你要把这个程序放到linux上用php -q执行那也要写出具体的$_SERVER['HTTP_USER_AGENT'],伪造的也可以   
  29.    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
  30.    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);   
  31.    curl_setopt($ch, CURLOPT_HEADER, false);//设定是否输出页面内容   
  32.    curl_setopt($ch, CURLOPT_GET, 1); // post,get 过去   
  33.   
  34.    $filecontent = curl_exec($ch);   
  35.    preg_match_all("/charset=(.+?)[NULL\"\']/is",$filecontent$charsetarray);   
  36.    if(strtolower($charsetarray[1][0])=="utf-8")   
  37.          $filecontent=iconv( 'utf-8''gb18030//IGNORE' , $filecontent);   
  38.    curl_close($ch);   
  39.    return $filecontent;   
  40. }   
  41.   
  42. ?>  
<?php   

function get_curlcuconent2($filename,$referer)
{
   $cookie_jar = tempnam('./tmp','JSESSIONID');
       
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $filename);
   curl_setopt($ch, CURLOPT_HEADER, false);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   //设置文件读取并提交的cookie路径
   curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
   $filecontent=curl_exec($ch);
   curl_close($ch);
       
   $ch = curl_init();
   $hostname ="www.domain.com";
   //$referer="http://www.domain.com/";
   curl_setopt($ch, CURLOPT_URL, $filename);
   curl_setopt($ch, CURLOPT_REFERER, $referer); // 看这里,你也可以说你从google来
   curl_setopt($ch, CURLOPT_USERAGENT, "www.domain.com");

   //$request = "JSESSIONID=abc6szw15ozvZ_PU9b-8r"; //设置POST参数
   //curl_setopt($ch, CURLOPT_POSTFIELDS, $request);   
   // 上面这句,当然你可以说你是baidu,改掉这里的值就ok了,可以实现小偷的功能,$_SERVER['HTTP_USER_AGENT']
   //你也可以自己做个 spider 了,那么就伪装这里的 CURLOPT_USERAGENT 吧
   //如果你要把这个程序放到linux上用php -q执行那也要写出具体的$_SERVER['HTTP_USER_AGENT'],伪造的也可以
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
   curl_setopt($ch, CURLOPT_HEADER, false);//设定是否输出页面内容
   curl_setopt($ch, CURLOPT_GET, 1); // post,get 过去

   $filecontent = curl_exec($ch);
   preg_match_all("/charset=(.+?)[NULL\"\']/is",$filecontent, $charsetarray);
   if(strtolower($charsetarray[1][0])=="utf-8")
         $filecontent=iconv( 'utf-8', 'gb18030//IGNORE' , $filecontent);
   curl_close($ch);
   return $filecontent;
}

?>
 


一种自定义:

 

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.   
  8. curl_setopt($curlHandel,CURLOPT_HTTPHEADER,$header);   
$header[]= 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, * '. '/* ';
$header[]= 'Accept-Language: zh-cn ';
$header[]= 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ';
$header[]= 'Host: '.$你的目标HOST;
$header[]= 'Connection: Keep-Alive ';
$header[]= 'Cookie: '.$你的COOKIE串;

curl_setopt($curlHandel,CURLOPT_HTTPHEADER,$header); 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值