libcurl
libcurl主要功能就是用不同的协议连接和沟通不同的服务器~也就是相当封装了的sockPHP 支持libcurl(允许你用不同的协议连接和沟通不同的服务器)。, libcurl当前支持http, https, ftp, gopher, telnet, dict, file, 和ldap 协议。libcurl同样支持HTTPS证书授权,HTTP POST, HTTP PUT, FTP 上传(当然你也可以使用PHP的ftp扩展), HTTP基本表单上传,代理,cookies,和用户认证。 官网
函数讲解可见: http://blog.csdn.net/haihsl123456789/article/details/41147949
Google瓦片抓取
瓦片相关信息可见:http://blog.csdn.net/mengdong_zy/article/details/50517682
实现代码如下:
#include <iostream>
#include <unistd.h>
#include <curl/curl.h>
#include <curl/easy.h>
using namespace std;
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
int written = fwrite(ptr,size,nmemb, (FILE*)stream);
return written;
}
int main(void)
{
CURL *curl;
curl_global_init(CURL_GLOBAL_ALL);
const string url = "http://mt2.google.cn/vt/lyrs=y@258000000&hl=zh-CN&gl=CN&src=app&x=214130&y=114212&z=18";
curl = curl_easy_init();
FILE *fp;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
if( (fp = fopen("/users/XXX/xxxx/test.jpg","ab")) == NULL )
{
curl_easy_cleanup(curl);
exit(1);
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");//这个要加不然文件传输不完全,字符串内容可自定义
double downloadFileLenth = 0;
if(CURLE_OK == curl_easy_perform(curl))
{
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &downloadFileLenth);
cout << " the file size is " << downloadFileLenth / 1024.0 << endl;
}
curl_easy_cleanup(curl);
return 0;
}