Jump to content

Get the HTML response from cURL / StdOutRead with a DLL (libcurl)


Recommended Posts

I'm using to upload a file to a server. After the upload the server returns a URL to that file. My program needs to give that URL to the user.

libcURL is working fine and the upload is successful, and the HTML response needed is output to scite's console. I need the information put into the console to be stored in a variable, though, and I can't figure it out.

I think I need to use stdout or stdread to do this. I have used these before when using a run() command but this library is using a dll (and is much cleaner than my previous version which used run commands).

How do I record the output buffer of a DLL? (or whatever stdout is called).

This attachment is what I'm testing it with, just open "playground.au3" and run it. It should spit a bunch of information to the console. The top listing of variables is all from curl_getdata(), the part with brackets and arrows is from my web server. I need the second part to be put into a variable.

PS: The cURL process ID is stored as $__hcurl_process

seangriffin's manages to accomplish this, it looks like the key is to use "DllCallbackRegister". I'm trying to plug this in to smartee's UDF but the two UDF's look like completely different languages. I don't think I will be able to figure this out on my own, but I'm going to keep trying.

EDIT: You need to move the DLL's to the same folder as playground.au3, and edit "libcURL.au3" and change the #include to quotes instead of brackets. I packed it wrong.

Edited by Rad
Link to comment
Share on other sites

Um, i don't see any attachment ;)

Here's a little demo showing how to save to a variable

#include <libcURL.au3>

Global $bMyData = Binary("")
_urlget("http://www.example.com")

ConsoleWrite(BinaryToString($bMyData))

Func _urlget($sURL)

    Local $curl_handle = _curl_easy_init()

    If ($curl_handle) Then

        Local $tCURLSTRUCT_URL = DllStructCreate("char[" & StringLen($sURL) + 1 & "]")
        DllStructSetData($tCURLSTRUCT_URL, 1, $sURL)
        _curl_easy_setopt($curl_handle, $CURLOPT_URL, DllStructGetPtr($tCURLSTRUCT_URL))

        _curl_easy_setopt($curl_handle, $CURLOPT_VERBOSE, 0)

        _curl_easy_setopt($curl_handle, $CURLOPT_FOLLOWLOCATION, 1)

        _curl_easy_setopt($curl_handle, $CURLOPT_NOPROGRESS, 1)

        Local $handle = DllCallbackRegister("_my_vwrite", "uint:cdecl", "ptr;uint;uint;ptr")
        _curl_easy_setopt($curl_handle, $CURLOPT_WRITEFUNCTION, DllCallbackGetPtr($handle))

        _curl_easy_perform($curl_handle)

        DllCallbackFree($handle)
        _curl_easy_cleanup($curl_handle)
    EndIf
EndFunc   ;==>_urlget

Func _my_vwrite($buffer, $size, $nmemb, $stream)
    Local $vData = DllStructCreate("byte[" & $size * $nmemb & "]", $buffer)
    $bMyData &= DllStructGetData($vData, 1)
    Return $size * $nmemb
EndFunc   ;==>_my_vwrite

Also you may want to take a look at the example for _curl_easy_setopt in the help-file, it shows how to save the output directly to a file.

Hope this helps :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...