LibCurl for AHK

Post your working scripts, libraries and tools for AHK v1.1 and older
mcl
Posts: 361
Joined: 04 May 2018, 16:35

LibCurl for AHK

26 Jul 2020, 20:38

Binding of LibCurl's easy interface for AHK, in case you need more control than WinINET/WinHTTP allows.
Provides functions for ease of use in most common HTTP operations: downloading to file/memory, POSTing forms, handling cookies and tracking progress.

Code: Select all

; Simple example
#Include Libcurl.ahk

; Don't forget to put dlls there!
Curl.Initialize(A_ScriptDir . "\libs")
; Curl.Deinitialize will be called automatically on exit.

myCurl := new Curl()
myCurl.SetUrl("http://example.com")
myCurl.WriteToFile("example_1_httpget.htm")
headBuffer := myCurl.HeaderToMem()
myCurl.Perform()

MsgBox % "Perform result:`n`n"
. myCurl.lastCode " : " myCurl.lastCodeText  ; Should be '0 : No error'

MsgBox % "Headers:`n`n"
. headBuffer.GetAsText("UTF-8")

MsgBox % "Info:`n"
. "`n" . myCurl.GetInfo(Curl.Info.EFFECTIVE_URL)
. "`n" . myCurl.GetInfo(Curl.Info.RESPONSE_CODE)
. "`n" . myCurl.GetInfo(Curl.Info.TOTAL_TIME_T) / 1000000
Required dll-libraries:
• libcurl
• openssl (libcrypto + libssl)

You can download them yourself (links can be found inside LibCurl.ahk file), or take a 7z-archive i prepared here: https://yadi.sk/d/jG67uSxLOHaJOQ
Archive includes both 32/64-bit libcurl and certificate bundle (from curl.se) and openssl libraries from François Piette, all digitally signed and tested.

UPDATED 2021-01-06:
Completely rewritten from scratch, fixed storing data in memory, fixed urlencoding/urldecoding, added human-readable error codes (via curl_easy_strerror), added extended curl_version_info, why do you read this, it's not that interesting, you'll probably won't need this anyway, more details in Opt/Info enum tables, some extra functions, handles now cleanup automatically, better callback mechanics and some other things i want to forget.
Attachments
LibCurl.zip
LibCurl.ahk + Examples
Dll-libraries not included.
(23.48 KiB) Downloaded 392 times
mcl
Posts: 361
Joined: 04 May 2018, 16:35

Re: LibCurl for AHK

06 Jan 2021, 13:54

I rewrote this completely, links and details are in the first post.

If you used it before − get the update, it's much better now (and sometimes even works!)
If you didn't used it − maybe you don't need it, but it's still much better now, just so you know.
If you don't know what it is − it's still a binding of libcurl, all that nerdy network stuff.
If you found a bug − let me know. I won't fix it, but it will lower my self-esteem, so give it a try.
If you have a github − please consider to adopt this ugly piece of code (and all the suffering that comes with it).
mstrauss2021
Posts: 41
Joined: 13 Feb 2021, 10:34

Re: LibCurl for AHK

20 Mar 2021, 17:23

In regards to your script above.

I am learning how to use libcurl and once in awhile I see a script that shows commands that I can't find anywhere else - like yours

Code: Select all

myCurl := new Curl()
myCurl.SetUrl
myCurl.WriteToFile
headBuffer := myCurl.HeaderToMem()
myCurl.Perform()
myCurl.GetInfo
Where can I fins information explaing these commands and showing me more?

I am trying to pull individual string values from website to make it easier to choose what I want and assign to variables. Currently, I can only grab the standard output which is everything.

Any help is appreciated

Mike
MrHue
Posts: 17
Joined: 01 Oct 2015, 02:51

Re: LibCurl for AHK

24 Jan 2022, 03:12

Thank you very much for sharing this library! I learnt a lot from your source!
I tested it very briefly.
Compared to WinInet / WinHttp / XmlHttp, some advantages of Libcurl are
- support for other protocols FTP/SMTP etc (WinInet also supports FTP)
- TLS1.3 support (Windows 11 supports it)
- decode gzip/deflate/broli (WinInet, Xmlhttp also supports it, WinHttp does not support br)
- has many options

Some disadvantages
- Does not support Unicode
- External Libcurl dll required
thanhkiem
Posts: 7
Joined: 23 Nov 2021, 23:20

Re: LibCurl for AHK

21 Jul 2022, 06:28

Code: Select all

Curl.Initialize("Libs")
; Create instance of curl's easy interface
myCurl := new Curl()

myCurl.SetUrl("ftp://xxxx.me/files/a.exe")
myCurl.SetOpt(Curl.Opt.USERNAME, "sammy")
myCurl.SetOpt(Curl.Opt.PASSWORD, "123456")
myCurl.SetOpt(Curl.Opt.UPLOAD, true)

Filename := A_ScriptDir . "\" . "MyIp.exe"
File := FileOpen(Filename, "r")
File.RawRead(BinData, FileLen := File.Length())

myCurl.SetOpt(Curl.Opt.READDATA, &BinData)
myCurl.SetOpt(Curl.Opt.VERBOSE, true)
myCurl.WriteToFile("ftp.txt")
myCurl.Perform()
MsgBox % "Example result:`n"
. "`n" . myCurl.lastCode " : " myCurl.lastCodeText " (from " . myCurl.lastCodeSource . ")"
I want to upload file with ftp but file upload allway have 0 kb.
Can anyone explain it to me?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: ArkuS and 246 guests