首页 > 代码库 > PHP curl几个实例

PHP curl几个实例

 1 <?php 2  3     //先学习一个简单的CURL,抓取无访问控制文件 4     //初始化curl,相当于一个句柄 5     $curl = curl_init(); 6      7     //设置你要抓取的网页 8     curl_setopt($curl,CURLOPT_URL,"http://www.baidu.com"); 9     10     //设置header,1头部可见,0头部不可见11     curl_setopt($curl,CURLOPT_HEADER,1);12     13     //设置返回状态,1返回到字符串,0返回到屏幕14     curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);15 16     //执行curl17     $data = curl_exec($curl);18 19     if(!file_exists(‘2.txt‘)){20         touch("2.txt");21     }else{22         file_put_contents(‘2.txt‘,$data);23     }24     25     curl_close($curl);26     27     //直接file_get_contents就能抓取没有访问控制的文件28     //echo file_get_contents("http://www.baidu.com/");29 30     $data = array(31         ‘practise‘ => ‘curl‘,32     );33     //接下来学习一下post提交数据34     $curl = curl_init();35     36     //提交url的地址,将表单提交到post.php上37     curl_setopt($curl,CURLOPT_URL,‘127.0.0.1/post.php‘);38 39     curl_setopt($curl,CURLOPT_POST,true);40 41     curl_setopt($curl,CURLOPT_POSTFIELDS,$data);42     43     curl_exec($curl);44     45     /*46     <?php 47     if($_POST){48         echo "<pre>";49         print_r($_POST);50         echo "</pre>";51     }52     ?>53     result:54     Array55     (56            [practise] => curl57     )58     */59 60     //接下来我们学习一下,代理服务器61     $ch = curl_init();62     curl_setopt($ch, CURLOPT_URL, ‘url‘);           //url设置为你要访问的地址63     curl_setopt($ch, CURLOPT_HEADER, 1);64     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);65     curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);66     curl_setopt($ch, CURLOPT_PROXY, ‘****‘);        //代理IP地址67     curl_setopt($ch, CURLOPT_PROXYUSERPWD, ‘user:password‘);68     $data = curl_exec();69     curl_close($ch);70     71         72 ?>

 

PHP curl几个实例