CURL函数

1、使用CURL模拟POST表单提交

Source.php 代码
  1. <?php  
  2. // 创建curl句柄  
  3. $ch = curl_init();  
  4. curl_setopt($ch,CURLOPT_URL,"http://localhost/curl/target.php" );  
  5. curl_setopt($ch,CURLOPT_POST,true);  
  6. $data = array('id' => 2 , 'name' => 'berry' );  
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获得返回内容,但不需要直接输出到页面上  
  8. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);   //POST 提交的参数写在一个数组里面  
  9. //curl_setopt($ch,CURLOPT_POSTFIELDS,"id=1&name=berry" ); //POST提交参数的另一种写法  
  10. $output = curl_exec($ch);   // 执行,并接收返回的内容  
  11. curl_close($ch);  
  12. echo $output;  
  13. ?>  
<?php
//创建curl句柄
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://localhost/curl/target.php");
curl_setopt($ch,CURLOPT_POST,true);
$data = array('id'=>2,'name'=>'berry');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);	//获得返回内容,但不需要直接输出到页面上
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);	 //POST提交的参数写在一个数组里面
//curl_setopt($ch,CURLOPT_POSTFIELDS,"id=1&name=berry"); //POST提交参数的另一种写法
$output = curl_exec($ch);	//执行,并接收返回的内容
curl_close($ch);
echo $output;
?>

 

Target.php 代码
  1. <?php  
  2. print_r($_POST);  
  3. ?>  
<?php
print_r($_POST);
?>

 

2、使用CURL判断一个页面是否存在

Php代码
  1. <?php  
  2. //创建句柄   
  3. $ch  = curl_init();  
  4. //设置参数   
  5. curl_setopt($ch , CURLOPT_URL, “http: //www.cnfol.com");   
  6. curl_setopt($ch , CURLOPT_RETURNTRANSFER, true);  
  7.   
  8. $output  = curl_exec( $ch );  
  9. //获取页面信息   
  10. $response_code  = curl_getinfo( $ch , CURLINFO_HTTP_CODE);  
  11.   
  12. if  ( $response_code  == ‘404′) {  
  13. echo  ‘页面不存在‘;  
  14. else  {  
  15. echo   $output ;  
  16. }  
  17. ?> 
  18. 原文转自http://berrywu.javaeye.com/blog/642834
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值