libcurl使用

libcurl主要功能就是用不同的协议连接和沟通不同的服务器~也就是相当封装了的sockPHP 支持libcurl(允许你用不同的协议连接和沟通不同的服务器)。, libcurl当前支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议。libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传(当然你也可以使用PHP的ftp扩展), HTTP基本表单上传,代理,cookies,和用户认证

 

这里,我们尝试使用curl实现邮件发送功能,使用example下的官方实例.在此之前,请阅读下面事项

 

* centos默认情况下可能安装可curl,使用curl -V 可以查看其支持的协议.

* curl 7.19.7 (i686-pc-linux-gnu) libcurl/7.19.7 NSS/3.12.7.0 zlib/1.2.3 libidn/1.18 libssh2/1.2.2

* Protocols: tftp ftp telnet dict ldap ldaps http file https ftps scp sftp

* Features: GSS-Negotiate IDN IPv6 Largefile NTLM SSL lib

* 这种情况下,不支持pop3 pop3s smtp smtps等协议.

 * 那么我们可以http://curl.haxx.se/libcurl下载最新的curl并且编译.

 * 但这个时候,为了和系统自带的做区分,需要在项目中指定自己编译的curl头和库地址:

* -I /usr/local/curl-7.28.0/include -L /usr/local/curl-7.28.0/lib -l curl

* 这样,可以编译通过,运行程序后,可能出现:

* curl_easy_perform() failed: Unsupported protocol 不支持协议,原因是,程序默认还是使用系统自带的curl库

* 需要配置指定运行库:/usr/local/curl-7.28.0/lib * OK 成功!

 

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

/**
 */
int main(void) {
    CURL *curl;
    CURLcode res;
    struct curl_slist *recipients = NULL;
    
    /* value for envelope reverse-path */
    static const char *from = "<bradh@example.com>";

    /* this becomes the envelope forward-path */
    static const char *to = "<bradh@example.net>";

    curl = curl_easy_init();
    if (curl) {
        /* this is the URL for your mailserver - you can also use an smtps:// URL
         * here */
        curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.net.");

        /* Note that this option isn't strictly required, omitting it will result in
         * libcurl will sent the MAIL FROM command with no sender data. All
         * autoresponses should have an empty reverse-path, and should be directed
         * to the address in the reverse-path which triggered them. Otherwise, they
         * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details.
         */
        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);

        /* Note that the CURLOPT_MAIL_RCPT takes a list, not a char array.  */
        recipients = curl_slist_append(recipients, to);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);

        /* You provide the payload (headers and the body of the message) as the
         * "data" element. There are two choices, either:
         * - provide a callback function and specify the function name using the
         * CURLOPT_READFUNCTION option; or
         * - just provide a FILE pointer that can be used to read the data from.
         * The easiest case is just to read from standard input, (which is available
         * as a FILE pointer) as shown here.
         */
        curl_easy_setopt(curl, CURLOPT_READDATA, stdin);

        /* send the message (including headers) */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        /* free the list of recipients */
        curl_slist_free_all(recipients);

        /* curl won't send the QUIT command until you call cleanup, so you should be
         * able to re-use this connection for additional messages (setting
         * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
         * curl_easy_perform() again. It may not be a good idea to keep the
         * connection open for a very long time though (more than a few minutes may
         * result in the server timing out the connection), and you do want to clean
         * up in the end.
         */
        curl_easy_cleanup(curl);
    }
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值