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

php中的curl使用心得详解

WBOY
Release: 2016-05-25 16:48:39
Original
1179 people have browsed it

这两天做的工作使用到了curl,当要请求的url和自己不在一台server上面,不能直接访问,这种情况下使用curl是最好不过了,模拟post请求做一些事,简单方便,下面记录一下在使用过程中的积累,实例代码如下:

<?php
/* 
远程post请求 
*/ 
function getRemoteUrl($get_url) { 
    $curl = curl_init();#启动一个CURL会话 
    curl_setopt($curl, CURLOPT_URL, $get_url);#设置一个Url 
    curl_setopt($curl, CURLOPT_POST, true);#发送一个常规的Post请求 
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);#设置超时限制防止死循环 
    curl_setopt($curl, CURLOPT_HEADER, 0);#显示返回的Header区域内容 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);#获取的信息以文件流的形式返回 
    $return = curl_exec($curl); #执行操作 
    if (curl_errno($curl)) { 
        return false; 
    } 
    curl_close($curl); #关闭CURL会话 
    return $return; 
} 
function getCurlData($url) { 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_TIMEOUT, 10); 
    $result = curl_exec($curl); 
    curl_close($curl); 
    return $result; 
}
?>
Copy after login

总结一下使用curl方法

先初始化curl,使用curl_setopt设置目标url,和其他选项 

curl_exec,执行curl 

执行后,关闭curl 

最后一步就是输出

curl()效率挺高的,支持多线程,不过需要开启下curl扩展,下面是curl扩展开启的步骤:

1、将PHP文件夹下的三个文件php_curl.dll,libeay32.dll,ssleay32.dll复制到system32下;

2、将php.ini(c:WINDOWS目录下)中的;extension=php_curl.dll中的分号去掉;

3、重启apache或者IIS


Related labels:
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!