Use gdal to obtain Network Data

Source: Internet
Author: User

Gdal provides an interface to access network data, the specific help documentation can access here (http://gdal.org/cpl__http_8h.html ). However, the premise is that gdal needs to rely on a third-party open-source library libcurl. The main function of libcurl is to use different protocols to connect and communicate with different servers ~ That is to say, sockphp supports libcurl (which allows you to connect to and communicate with different servers using different protocols )., Libcurl currently supports HTTP, https, FTP, Gopher, telnet, dict, file, and LDAP protocols. Libcurl also supports HTTPS certificate authorization, http post, http put, FTP upload (of course, you can also use php ftp extension), HTTP basic form upload, proxy, cookies, and user authentication.
For details, visit libcurl's Wikipedia. The link is here.

Well, the following describes how to use it. First, use the cplhttpenabled () function to check whether gdal supports libcurl, and then use the following code to obtain the URL returned information. (The following URL is the profile picture URL of my blog ).

CPLHTTPResult *pRest = CPLHTTPFetch ("http://avatar.csdn.net/7/0/7/1_liminlu0314.jpg", NULL);

Then, it is determined that the release of Prest is null. If it is not null, the information is obtained successfully and the obtained information is stored in prest. Then retrieve the content from the prest. The following is a small function encapsulated by me that can be used to obtain images or files from the network and store them locally.

bool SaveUrl2File(const char* pszUrl, const char* pszFileName){if( CPLHTTPEnabled() != TRUE)return false;CPLHTTPResult *pRest = CPLHTTPFetch (pszUrl, NULL);if (pRest == NULL)return false;FILE *pFile = fopen(pszFileName, "wb");if (pFile == NULL)return false;fwrite( pRest->pabyData, sizeof( GByte ), pRest->nDataLen, pFile );fclose(pFile);CPLHTTPDestroyResult(pRest);CPLHTTPCleanup();return true;}

It is easy to use. For example, you can use the following code to download an image or mp3 file.

bool bIsSuccess = SaveUrl2File("http://avatar.csdn.net/7/0/7/1_liminlu0314.jpg", "C:\\photo.jpg");bIsSuccess = SaveUrl2File("http://music.charlottedann.com/songs/Safe%20And%20Sound.mp3", " Safe & Sound Taylor Swift mp3");

The above one is to download my csdn avatar, and the other is to download the ending song "Safe and sound" of "Hunger Game". ps, nice to listen to. I recommend it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.