码迷,mamicode.com
首页 > Web开发 > 详细

curl常用功能

时间:2017-01-08 17:26:18      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:数组   调试   释放   相关   读取   length   ica   span   调用   

 1 <?php 
 2  //创建一个新cURL资源
 3 $ch = curl_init();
 4 //************************************************************************************
 5 //设置URL和相应的选项
 6    //设置 HTTP 头字段的数组。格式: array(‘Content-type: text/plain‘, ‘Content-length: 100‘) 
 7  $headers  = array(
 8 "content-type: application/x-www-form-urlencoded; charset=gb2312");
 9 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
10 curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/");
11 //************************************************************************************
12 //索要上传的数据 注意:要发送文件时,要在文件名前面加上  @  前缀并使用完整路径
13 $data = array(‘name‘=>‘beauty‘, "upload"=>"@a.zip");
14 curl_setopt($ch, CURLOPT_POST, 1);
15 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
16 //************************************************************************************
17 ////设置curl参数,要求结果是否输出到屏幕上,为1的时候是不返回到网页中,0:直接显示 假设的0换成1的话,那么接下来的$data就需要echo一下。
18 curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);//是否自动显示返回的信息 
19 //是否显示返回的http头信息
20 curl_setopt($ch, CURLOPT_HEADER, 1);
21 //************************************************************************************
22 
23 // 连接结束后,比如,调用 curl_close 后,保存 cookie 信息的文件。  
24 curl_setopt($ch,CURLOPT_COOKIEJAR, dirname(__FILE__).‘/cookie.txt‘); 
25 //************************************************************************************
26 //************************************************************************************
27 
28 curl_setopt($curl, CURLOPT_POST, 1);//post方式提交 
29  curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息 
30 //************************************************************************************
31  
32  //允许 cURL 函数执行的最长秒数。 
33  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
34 //************************************************************************************
35  
36  curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).‘/cookie.txt‘); //读取cookie
37 //************************************************************************************
38 
39 //curl_getinfo()函数返回cURL执行后这一请求相关的信息,这对调试和排错很有用: 取得对方响应后的信息
40 $info = curl_getinfo($ch);
41 //************************************************************************************
42 
43 // 抓取URL并把它传递给浏览器
44 $res = curl_exec($ch);
45 
46 // 关闭cURL资源,并且释放系统资源
47 curl_close($ch);
48     echo "<pre>";
49        print_r($info);
50     echo "</pre>";
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72  ?>

 

curl常用功能

标签:数组   调试   释放   相关   读取   length   ica   span   调用   

原文地址:http://www.cnblogs.com/boundless-sky/p/6262232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!