libcurl
一、libcurl
主要功能就是用不同的协议连接和沟通不同的服务器~也就是相当封装了的sockPHP 支持libcurl(允许你用不同的协议连接和沟通不同的服务器)。, libcurl当前支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议。libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传(当然你也可以使用PHP的ftp扩展), HTTP基本表单上传,代理,cookies,和用户认证。
二、利用libcurl 下载文件代码例程
#include <stdio.h> #include <curl/curl.h> //#include <curl/types.h> /* new for v7 */ #include <curl/easy.h> /* new for v7 */ ////这个函数是为了符合CURLOPT_WRITEFUNCTION而构造的 //完成数据保存功能 size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream) { return fwrite(ptr, size, nmemb, stream); } //这个函数是为了符合CURLOPT_READFUNCTION而构造的 //数据上传时使用 size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream) { return fread(ptr, size, nmemb, stream); } //这个函数是为了符合CURLOPT_PROGRESSFUNCTION而构造的 //显示文件传输进度,t代表文件大小,d代表传 输已经完成部分 int my_progress_func(void *bar, double t, /* dltotal */ double d, /* dlnow */ double ultotal, double ulnow) { printf("%f / %f (%g %%)\n", d, t, d*100.0/t); //printf("\r\033[?25l%f / %f (%g %%)\033[?25h", d, t, d*100.0/t); return 0; } void *my_thread(void *ptr) { CURL *curl; CURLcode res; FILE *outfile; char *url = ptr; curl = curl_easy_init(); if(curl) { outfile = fopen("test.zip", "w"); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write_func); curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_read_func); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, my_progress_func); // curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, Bar); res = curl_easy_perform(curl); fclose(outfile); /* always cleanup */ curl_easy_cleanup(curl); } return NULL; } int main(int argc, char **argv) { /* Must initialize libcurl before any threads are started */ curl_global_init(CURL_GLOBAL_ALL); //if (!g_thread_create(&my_thread, argv[1], false, NULL) != 0) //g_warning("can't create the thread"); my_thread(argv[1]); while(1) { sleep(2); } return 0; }
三、curl_easy_perform(curl) 返回值
CURLE_OK = 0, 0: no error CURLE_UNSUPPORTED_PROTOCOL, 1: unsupported protocol CURLE_FAILED_INIT, 2: failed init CURLE_URL_MALFORMAT, 3: URL using bad/illegal format or missing URL CURLE_URL_MALFORMAT_USER, 4: unknown error CURLE_COULDNT_RESOLVE_PROXY, 5: couldn’t resolve proxy name CURLE_COULDNT_RESOLVE_HOST, 6: couldn’t resolve host name CURLE_COULDNT_CONNECT, 7: couldn’t connect to server CURLE_FTP_WEIRD_SERVER_REPLY, 8: FTP: weird server reply CURLE_FTP_ACCESS_DENIED,9 CURLE_FTP_USER_PASSWORD_INCORRECT, 10: unknown error CURLE_FTP_WEIRD_PASS_REPLY, 11: FTP: unknown PASS reply CURLE_FTP_WEIRD_USER_REPLY, 12: FTP: unknown USER reply CURLE_FTP_WEIRD_PASV_REPLY, 13: FTP: unknown PASV reply CURLE_FTP_WEIRD_227_FORMAT, 14: FTP: unknown 227 response format CURLE_FTP_CANT_GET_HOST, 15: FTP: can’t figure out the host in the PASV response CURLE_FTP_CANT_RECONNECT, 16: FTP: can’t connect to server the response code is unknown CURLE_FTP_COULDNT_SET_BINARY, 17: FTP: couldn’t set binary mode CURLE_PARTIAL_FILE, 18: Transferred a partial file CURLE_FTP_COULDNT_RETR_FILE, 19: FTP: couldn’t retrieve (RETR failed) the specified file CURLE_FTP_WRITE_ERROR, 20: FTP: the post-transfer acknowledge response was not OK CURLE_FTP_QUOTE_ERROR, 21: FTP: a quote command returned error CURLE_HTTP_RETURNED_ERROR, 22: HTTP response code said error CURLE_WRITE_ERROR, 23: failed writing received data to disk/application CURLE_MALFORMAT_USER, 24: unknown error CURLE_UPLOAD_FAILED, 25: upload failed (at start/before it took off) CURLE_READ_ERROR, 26: failed to open/read local data from file/application CURLE_OUT_OF_MEMORY, 27: out of memory CURLE_OPERATION_TIMEOUTED, 28: a timeout was reached CURLE_FTP_COULDNT_SET_ASCII, 29: FTP could not set ASCII mode (TYPE A) CURLE_FTP_PORT_FAILED, 30: FTP command PORT failed CURLE_FTP_COULDNT_USE_REST, 31: FTP command REST failed CURLE_FTP_COULDNT_GET_SIZE, 32: FTP command SIZE failed CURLE_HTTP_RANGE_ERROR, 33: a range was requested but the server did not deliver it CURLE_HTTP_POST_ERROR, 34: internal problem setting up the POST CURLE_SSL_CONNECT_ERROR, 35: SSL connect error CURLE_BAD_DOWNLOAD_RESUME, 36: couldn’t resume download CURLE_FILE_COULDNT_READ_FILE, 37: couldn’t read a file CURLE_LDAP_CANNOT_BIND, 38: LDAP: cannot bind CURLE_LDAP_SEARCH_FAILED, 39: LDAP: search failed CURLE_LIBRARY_NOT_FOUND, 40: a required shared library was not found