zoukankan      html  css  js  c++  java
  • About libcurl and cURL in PHP

    今天在学习php时遇到要调用curl 库函数对特定url字符串进行访问操作,需要自己写一个方法进行调用,之前在linux系统中也有用到cURL 命令行工具执行对相关资源的获取,在wiki上找到了如下的介绍:

    另外php手册关于php client URL函数链接:http://php.net/manual/zh/function.curl-setopt.php

    PHP 支持 Daniel Stenberg 创建的 libcurl 库,能够连接通讯各种服务器使用各种协议。libcurl 目前支持的协议有 http、https、ftp、gopher、telnet、dict、file、ldap。 libcurl 同时支持 HTTPS 证书、HTTP POST、HTTP PUT、 FTP 上传(也能通过 PHP 的 FTP 扩展完成)、HTTP 基于表单的上传、代理、cookies、用户名+密码的认证。

    从上面的介绍可以看出curl库函数客户端通过URL连接服务器的,它支持各种协议和认证

    使用libcurl库函数,使得客户端和服务器间能够进行cURL会话

    PHP中定义的cURL函数有以下一些:

    cURL 函数 ¶

    Table of Contents ¶

     以下是wiki的基本介绍:

    cURL (/kɝl/ or /kə:l/[4]) is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997. The name originally stood for "see URL". The original author and lead developer is the Swedish developer Daniel Stenberg.[1]

    libcurl[edit]

    libcurl is a free client-side URL transfer library, supporting cookies, DICT, FTP, FTPS, Gopher, HTTP (with HTTP/2 support), HTTP POST, HTTP PUT, HTTP proxy tunneling, HTTPS, IMAP, Kerberos, LDAP, POP3, RTSP, SCP, and SMTP. The library supports the file URI scheme, SFTP, Telnet, TFTP, file transfer resume, FTP uploading, HTTP form-based upload, HTTPS certificates, LDAPS, proxies, and user-plus-password authentication.

    The libcurl library is portable. It builds and works identically on many platforms, including AIX, AmigaOS, Android, BeOS, BlackBerry Tablet OS and BlackBerry 10,[5] OpenVMS, Darwin, DOS, FreeBSD, HP-UX, HURD, iOS, IRIX, Linux, macOS, NetBSD, NetWare, OpenBSD, OS/2, QNX Neutrino, RISC OS, Solaris, Symbian, Tru64, Ultrix, UnixWare, and Windows.

    The libcurl library is free, thread-safe and IPv6 compatible. Bindings are available for more than 40 languages, including C/C++, Java, PHP and Python.

    The libcurl library can support axTLS, GnuTLS, mbed TLS, NSS, QSOSSL on IBM i, SChannel on Windows, Secure Transporton macOS and iOS, SSL/TLS through OpenSSL, and wolfSSL.

     

    cURL[edit]

    cURL is a command line tool for getting or sending files using URL syntax.

    Since cURL uses libcurl, it supports a range of common Internet protocols, currently including HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, LDAP, DAP, DICT, TELNET, FILE, IMAP, POP3, SMTP and RTSP (the last four only in versions newer than 7.20.0 or 9 February 2010).

    cURL supports HTTPS and performs SSL certificate verification by default when a secure protocol is specified such as HTTPS. When cURL connects to a remote server via HTTPS, it will first obtain the remote server certificate and check against its CA certificate store the validity of the remote server to ensure the remote server is the one it claims to be. Some cURL packages have bundled with CA certificate store file. There are few options to specify CA certificate such as --cacert and --capath. --cacert option can be used to specify the location of the CA certificate store file. In the Windows platform, if a CA certificate file is not specified, cURL will look for a CA certificate file name “curl-ca-bundle.crt” in the following order:

    1. Directory where the cURL program is located.
    2. Current working directory.
    3. Windows system directory.
    4. Windows directory.
    5. Directories specified in the %PATH% environment variables.[6]

    cURL will return an error message if the remote server is using a self-signed certificate, or if the remote server certificate is not signed by a CA listed in the CA cert file. -k or --insecure option can be used to skip certificate verification. Alternatively, if the remote server is trusted, the remote server CA certificate can be added to the CA certificate store file.

    Examples of cURL use from command line[edit]

    Basic use of cURL involves simply typing curl at the command line, followed by the URL of the output to retrieve.

    To retrieve the example.com homepage, type:

    curl www.example.com
    

    cURL defaults to displaying the output it retrieves to the standard output specified on the system (usually the terminal window). So running the command above would, on most systems, display the www.example.com source-code in the terminal window.

    cURL can write the output it retrieves to a file with the -o flag, thus:

    curl -o example.html www.example.com
    

    This will store the source code for www.example.com into a file named example.html. While retrieving output, cURL will display a progress bar showing how much of the output has downloaded. Note however that cURL does not show a progress bar when preparing to display the output in the terminal window, since a progress bar is likely to interfere with the display of the output.

    To download output to a file that has the same name as on the system it originates from, use the -O flag, for example:

    curl -O www.example.com/example.html
    

    If the server responds that the file (example.html) is moved to a different location (indicated with a Location: header and a 3XX response code), use the -L flag, for example:

    curl -OL www.example.com/example.html
    

    cURL can connect to a remote server via HTTPS protocol (return error message if a CA certificate file cannot be located)

    curl https://securesite.com/login.html
    

    To specify a CA certificate file:

    curl --cacert c:	empcacerts.crt https://securesite.com/login.html
    

    To skip certificate verification:

    curl --insecure https://self-signed-cert.com/login.html
    

    Curl offers many other features, including proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume, and Metalink.

     项目中的例子:

     

    /**

    *在这里我们自定义定义特定URL会话的方法

    *@param  $url  要访问的url

    *@param  int  $type  0get  1post

    *@param  array   $data  要传输的数据,这里默认为空

    */

    function doCurl($url,$type=0,$data=[]){

    $ch=curl_init();//由curl_init()返回的cURL句柄

    curl_setopt($ch,CURLOPT_URL,$url);

    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//TRUE将curl_exec()获取的信息以字符串返回,而不是直接输出。

    curl_setopt($ch,CURLOPT_HEADER,0);//启用时会将header头文件的信息作为数据流输出。这里不启用

    if($type==1){

    curl_setopt($ch,CURLOPT_POST,1);

    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

    }

    //执行cURL会话并获取内容

    $output=curl_exec($ch);

    //关闭cURL会话(释放句柄)

    curl_close($ch);

    return  $output;

    }

     

    /extend/Map.php:

    publicstatic     function   getLngLat($address){

    //http://api.map.baidu.com/geocoder/v2/?address=北京市海淀区上地十街10号&output=json

    //&ak=您的ak&callback=showLocation//GET请求

    拼接参数

    $data=[

    'address'=>$address,

    'ak'=>config('map.ak'),

    'output'=>'json',

    ];

     

     

    //拼接url地址字符串

    $url=config('map.baidu_map_url').config('map.geocoder').'?'.http_build_query($data);

    这里config(map.)方法表示使用extra/map.php中的相关值

    http_build_query()将上述参数数组改造成GET参数的形式

     

    //获取$url的内容以下两种方法

    //1.file_get_content($url)

    //2.php中的libcurl需要自己封装一个方法进行会话

    $result=doCurl($url);

    print_r($result);

    }

  • 相关阅读:
    Linux下动态库(.so)和静态库(.a) 的区别
    CTS、CLS、CLR
    ASP.NET简介及网页基础知识
    ASP.NET MVC中ActionResult的不同返回方式
    ADO.NET中的数据库帮助类
    ASP.NET MVC 方法View返回的两种方式
    使用win10 IIS 发布局域网网站
    ASP.NET MVC 给Action的参数赋值的方式
    ASP.NET MVC简单流程解释(传值方式)
    ASP.NET MVC 简介(附VS2019和VSCode版示例)
  • 原文地址:https://www.cnblogs.com/loanhicks/p/8017154.html
Copyright © 2011-2022 走看看