curl_easy_init

curl_easy_init erzeugt einen curl-Pointer, der mit der Funktion curl_easy_cleanup wieder zerstört werden kann.

curl = curl_easy_init()

Returnwert

Ist ein Curl-Pointer, der mit der Funktion curl_easy_cleanup wieder zerstört werden kann.

Kommentar

Die curl-Funktionen verwendet die cURL-Library, siehe http://curl.haxx.se/.

Beispiel

Beispiel 1: Download einer utf8-Textdatei.

curl_download_text("http://www.uniplot.de")

def _DownloadCBData(data, user)
{
    user.cnt = user.cnt + 1;
    user.data[user.cnt] = data;
    return 1;
}

def curl_download_text(ssURL)
{
    curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, ssURL);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, "_DownloadCBData");

    o = [. cnt = 0, data = [.]];
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, o);

    r = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    if (r != 0) {
        return error_create("CURL", r, curl_easy_strerror(r));
    }
    s = "";
    if (o.cnt == 0) {
        return s;
    }
    for (i in 1:o.cnt) {
        s = s + mem_unpack(o.data[i]);
    }
    return utf8_decode(s);
}

Beispiel 2: Upload einer Datei mit dem FTP-Protokoll:

def _curl_upload_callback(nBytes, oUser)
{
    return fread_char8(oUser.fp, nBytes);
}

def curl_upload_test(ssFile, ssURL, oOptions)
{
    ssFilename = sum(SplitPath(ssFile)[3:4]);
    _ssURL = ssURL + ssFilename;

    curl = curl_easy_init();

    curl_easy_setopt(curl, oOptions);
    curl_easy_setopt(curl, CURLOPT_URL, _ssURL);

    curl_easy_setopt(curl, CURLOPT_READFUNCTION, "_curl_upload_callback");

    fp = fopen(ssFile, "rb");
    if (fp == 0) {
        return error_create("file", 1, "Cannot open file");;
    }

    oUser = [. fp = fp];
    curl_easy_setopt(curl, CURLOPT_READDATA, oUser);
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);

    /* Set the size of the file to upload */
    n = fgetlen(fp);
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, n);

    r = curl_easy_perform(curl);

    curl_easy_cleanup(curl);

    fclose(fp);

    if (r != 0) {
        return error_create("CURL", r, curl_easy_strerror(r));
    }
    return 0;
}

 curl_upload_test("d:\\test.txt", "ftp://www.example.com/", [. username = "Peter", password = "Geheim", timeout=5])

History

Version Beschreibung
R2015.0 Neu.

id-1511818