Heim  >  Artikel  >  Backend-Entwicklung  >  php中的curl使用心得详解_PHP教程

php中的curl使用心得详解_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:16:31866Durchsuche

php中的curl我前面就讲过一篇这这样的文件,下面我来把我学习php curl使用心得给大家分享一下,希望这些对大家会有所帮助。

这两天做的工作使用到了curl

当要请求的url和自己不在一台server上面,不能直接访问

这种情况下使用curl是最好不过了,模拟post请求做一些事,简单方便

下面记录一下在使用过程中的积累

 代码如下 复制代码

/*
远程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;
}

总结一下使用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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/628613.htmlTechArticlephp中的curl我前面就讲过一篇这这样的文件,下面我来把我学习php curl使用心得给大家分享一下,希望这些对大家会有所帮助。 这两天做的工...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn