Home > php教程 > php手册 > body text

PHP:简单的CURL应用实例

WBOY
Release: 2016-06-06 20:09:21
Original
948 people have browsed it

作为一个PHP开发人员或者说经常接触PHP源码的人来说,CURL简直是太熟悉了。不过说起来,你真的知道CURL吗,别不是你只懂得它用来干什么的吧,还是说只是见过而已呢。其实呢,CURL组件经常会见到,也确实被广泛地应用在很多的地方。例如:抓取页面,向页面提

作为一个PHP开发人员或者说经常接触PHP源码的人来说,CURL简直是太熟悉了。不过说起来,你真的知道CURL吗,别不是你只懂得它用来干什么的吧,还是说只是见过而已呢。其实呢,CURL组件经常会见到,也确实被广泛地应用在很多的地方。例如:抓取页面,向页面提交数据等。可以说,CURL同其他热门应用一样,是我们必须掌握的。下面,我们就来看个利用CURL抓取页面的实例吧:

function curl_download($Url){    
    // 为了防止错误 先检查是否安装了CURL组件   
    if (!function_exists(‘curl_init’)){   
        die(对不起,没有安装CURL组件呢!’);   
    }    
    // 好的,既然安装了我们新建一个handle句柄   
    $ch = curl_init();    
    // 设置一下 很多都是可选的    
    // 设置要抓取的地址   
    curl_setopt($ch, CURLOPT_URL, $Url);    
    //建立一个http Referer 有些网站会判断请求来路 我们可以伪造啊   
    curl_setopt($ch, CURLOPT_REFERER, “http://www.example.org/yay.htm”);    
    // 设置用户代理   
    curl_setopt($ch, CURLOPT_USERAGENT, “MozillaXYZ/1.0″);    
    //包含header头部标识吗 (0 = yes, 1 = no)   
    curl_setopt($ch, CURLOPT_HEADER, 0);    
    //决定是返回数据还是打印出来 (true = return, false = print)   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
    // 超时的秒数   
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);    
    // 开始抓取 并返回相应数据   
    $output = curl_exec($ch);    
    // 关闭连接   
    curl_close($ch);    
    return $output;   
}  
Copy after login

更多关于CURL的应用,可以到Client URL Library了解……

转载请注明:IT路人 » PHP:简单的CURL应用实例

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!