2014-06-17 02:27:37 +08:00
|
|
|
.\" **************************************************************************
|
|
|
|
.\" * _ _ ____ _
|
|
|
|
.\" * Project ___| | | | _ \| |
|
|
|
|
.\" * / __| | | | |_) | |
|
|
|
|
.\" * | (__| |_| | _ <| |___
|
|
|
|
.\" * \___|\___/|_| \_\_____|
|
|
|
|
.\" *
|
2023-01-02 20:51:48 +08:00
|
|
|
.\" * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2014-06-17 02:27:37 +08:00
|
|
|
.\" *
|
|
|
|
.\" * This software is licensed as described in the file COPYING, which
|
|
|
|
.\" * you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
.\" * are also available at https://curl.se/docs/copyright.html.
|
2014-06-17 02:27:37 +08:00
|
|
|
.\" *
|
|
|
|
.\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
.\" * copies of the Software, and permit persons to whom the Software is
|
|
|
|
.\" * furnished to do so, under the terms of the COPYING file.
|
|
|
|
.\" *
|
|
|
|
.\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
.\" * KIND, either express or implied.
|
|
|
|
.\" *
|
|
|
|
.\" * SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
.\" *
|
2014-06-17 02:27:37 +08:00
|
|
|
.\" **************************************************************************
|
|
|
|
.\"
|
2023-04-26 14:58:35 +08:00
|
|
|
.TH CURLOPT_WRITEFUNCTION 3 "16 Jun 2014" libcurl libcurl
|
2014-06-17 02:27:37 +08:00
|
|
|
.SH NAME
|
2021-09-09 04:55:58 +08:00
|
|
|
CURLOPT_WRITEFUNCTION \- callback for writing received data
|
2014-06-17 02:27:37 +08:00
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);
|
|
|
|
|
|
|
|
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEFUNCTION, write_callback);
|
|
|
|
.SH DESCRIPTION
|
2014-06-17 05:55:30 +08:00
|
|
|
Pass a pointer to your callback function, which should match the prototype
|
|
|
|
shown above.
|
2014-06-17 02:27:37 +08:00
|
|
|
|
|
|
|
This callback function gets called by libcurl as soon as there is data
|
2018-11-23 23:55:33 +08:00
|
|
|
received that needs to be saved. For most transfers, this callback gets called
|
|
|
|
many times and each invoke delivers another chunk of data. \fIptr\fP points to
|
|
|
|
the delivered data, and the size of that data is \fInmemb\fP; \fIsize\fP is
|
|
|
|
always 1.
|
2014-06-17 02:27:37 +08:00
|
|
|
|
2023-08-22 23:40:39 +08:00
|
|
|
The callback function is passed as much data as possible in all invokes, but
|
|
|
|
you must not make any assumptions. It may be one byte, it may be
|
|
|
|
thousands. The maximum amount of body data that is be passed to the write
|
2014-06-17 06:02:22 +08:00
|
|
|
callback is defined in the curl.h header file: \fICURL_MAX_WRITE_SIZE\fP (the
|
|
|
|
usual default is 16K). If \fICURLOPT_HEADER(3)\fP is enabled, which makes
|
|
|
|
header data get passed to the write callback, you can get up to
|
|
|
|
\fICURL_MAX_HTTP_HEADER\fP bytes of header data passed into it. This usually
|
|
|
|
means 100K.
|
|
|
|
|
|
|
|
This function may be called with zero bytes data if the transferred file is
|
|
|
|
empty.
|
|
|
|
|
2023-08-22 23:40:39 +08:00
|
|
|
The data passed to this function is not null-terminated!
|
2014-06-17 06:02:22 +08:00
|
|
|
|
|
|
|
Set the \fIuserdata\fP argument with the \fICURLOPT_WRITEDATA(3)\fP option.
|
2014-06-17 02:27:37 +08:00
|
|
|
|
2014-06-17 05:55:30 +08:00
|
|
|
Your callback should return the number of bytes actually taken care of. If
|
2023-08-22 23:40:39 +08:00
|
|
|
that amount differs from the amount passed to your callback function, it
|
|
|
|
signals an error condition to the library. This causes the transfer to get
|
|
|
|
aborted and the libcurl function used returns \fICURLE_WRITE_ERROR\fP.
|
2014-06-17 02:27:37 +08:00
|
|
|
|
2022-11-09 07:49:21 +08:00
|
|
|
You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0)
|
|
|
|
|
2023-08-22 23:40:39 +08:00
|
|
|
If the callback function returns CURL_WRITEFUNC_PAUSE it pauses this
|
|
|
|
transfer. See \fIcurl_easy_pause(3)\fP for further details.
|
2014-06-17 02:27:37 +08:00
|
|
|
|
|
|
|
Set this option to NULL to get the internal default function used instead of
|
2023-08-22 23:40:39 +08:00
|
|
|
your callback. The internal default function writes the data to the FILE *
|
2014-06-17 02:27:37 +08:00
|
|
|
given with \fICURLOPT_WRITEDATA(3)\fP.
|
2020-11-03 06:17:01 +08:00
|
|
|
|
2021-10-31 23:34:44 +08:00
|
|
|
This option does not enable HSTS, you need to use \fICURLOPT_HSTS_CTRL(3)\fP to
|
2020-11-03 06:17:01 +08:00
|
|
|
do that.
|
2014-06-17 04:01:08 +08:00
|
|
|
.SH DEFAULT
|
2023-08-22 23:40:39 +08:00
|
|
|
libcurl uses 'fwrite' as a callback by default.
|
2014-06-17 02:27:37 +08:00
|
|
|
.SH PROTOCOLS
|
|
|
|
For all protocols
|
|
|
|
.SH EXAMPLE
|
2020-08-24 16:00:13 +08:00
|
|
|
.nf
|
2023-02-01 20:59:25 +08:00
|
|
|
struct memory {
|
|
|
|
char *response;
|
|
|
|
size_t size;
|
|
|
|
};
|
2020-04-07 00:14:10 +08:00
|
|
|
|
2023-02-07 22:58:17 +08:00
|
|
|
static size_t cb(void *data, size_t size, size_t nmemb, void *clientp)
|
2023-02-01 20:59:25 +08:00
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
2023-02-07 22:58:17 +08:00
|
|
|
struct memory *mem = (struct memory *)clientp;
|
2020-04-07 00:14:10 +08:00
|
|
|
|
2023-02-01 20:59:25 +08:00
|
|
|
char *ptr = realloc(mem->response, mem->size + realsize + 1);
|
|
|
|
if(ptr == NULL)
|
|
|
|
return 0; /* out of memory! */
|
2020-04-07 00:14:10 +08:00
|
|
|
|
2023-02-01 20:59:25 +08:00
|
|
|
mem->response = ptr;
|
|
|
|
memcpy(&(mem->response[mem->size]), data, realsize);
|
|
|
|
mem->size += realsize;
|
|
|
|
mem->response[mem->size] = 0;
|
2020-04-07 00:14:10 +08:00
|
|
|
|
2023-02-01 20:59:25 +08:00
|
|
|
return realsize;
|
|
|
|
}
|
2020-04-07 00:14:10 +08:00
|
|
|
|
2023-02-01 20:59:25 +08:00
|
|
|
struct memory chunk = {0};
|
|
|
|
CURLcode res;
|
|
|
|
CURL *curl_handle = curl_easy_init();
|
2020-04-07 00:14:10 +08:00
|
|
|
|
2023-02-01 20:59:25 +08:00
|
|
|
if (curl_handle)
|
|
|
|
{
|
|
|
|
/* send all data to this function */
|
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, cb);
|
2020-04-07 00:14:10 +08:00
|
|
|
|
2023-02-01 20:59:25 +08:00
|
|
|
/* we pass our 'chunk' struct to the callback function */
|
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
|
|
|
|
|
|
|
|
/* send a request */
|
|
|
|
res = curl_easy_perform(curl_handle);
|
|
|
|
|
|
|
|
/* remember to free the buffer */
|
2023-03-26 03:54:02 +08:00
|
|
|
free(chunk.response);
|
2023-02-01 20:59:25 +08:00
|
|
|
|
|
|
|
curl_easy_cleanup(curl_handle);
|
|
|
|
}
|
2020-08-24 16:00:13 +08:00
|
|
|
.fi
|
2021-09-01 16:26:36 +08:00
|
|
|
.SH AVAILABILITY
|
|
|
|
Support for the CURL_WRITEFUNC_PAUSE return code was added in version 7.18.0.
|
|
|
|
.SH RETURN VALUE
|
2023-08-22 23:40:39 +08:00
|
|
|
This returns CURLE_OK.
|
2014-06-17 02:27:37 +08:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR CURLOPT_WRITEDATA "(3), " CURLOPT_READFUNCTION "(3), "
|
2020-04-07 00:14:10 +08:00
|
|
|
.BR CURLOPT_HEADERFUNCTION "(3), "
|