Tom van der Woerdt

NAME

AnyEvent::YACurl - Yet Another cURL binding for AnyEvent

SYNOPSIS

    use AnyEvent;
    use AnyEvent::YACurl ':constants';

    my $client = AnyEvent::YACurl->new({});
    my $condvar = AnyEvent->condvar;
    my $return_data = '';
    $client->request($condvar, {
        CURLOPT_URL => "https://www.perl.org",
        CURLOPT_VERBOSE => 1,
        CURLOPT_WRITEFUNCTION => sub {
            my ($chunk) = @_;
            $return_data .= $chunk;
        }
    });

    my ($response, $error) = $condvar->recv;
    my $response_code = $response->getinfo(CURLINFO_RESPONSE_CODE);
    print "Have response code $response_code. Body was $return_data";

DESCRIPTION

This module provides bindings to cURL, integrated into AnyEvent.

AnyEvent::YACurl methods

new

Returns a new AnyEvent::YACurl object. This is essentially a binding over cURL's "multi" interface.

Its first and only argument is a required hashref containing options to control behavior, such as CURLMOPT_MAX_TOTAL_CONNECTIONS. Refer to the actual cURL documentation to find out about other options to pass.

request

Performs a request using the client instantiated via new. Takes a callback and a hashref of cURL options (CURLOPT_*). At a minimum CURLOPT_URL must be provided, but it's recommended to pass a few more arguments than that. Refer to the actual cURL documentation to find out about other options to pass.

request does not return anything, but will invoke the coderef passed via callback once the request is completed or had an error. The callback is invoked with two arguments, response and error, but only one of the two will be defined.

The response argument to the callback is a AnyEvent::YACurl::Response object, documented later in this pod, unless there was an error. If that was the case, the error argument to the callback will contain a human readable description of what went wrong.

    use Promises qw/deferred/;

    my $deferred = deferred;
    $client->request(
        sub {
            my ($response, $error) = @_;
            if ($error) {
                $deferred->reject($error);
            } else {
                $deferred->resolve($response->getinfo(CURLINFO_RESPONSE_CODE));
            }
        },
        {
            CURLOPT_URL => "https://www.perl.org",
            ...
        }
    );

AnyEvent::YACurl::Response methods

getinfo

Queries the cURL API for information about the response. Refer to the cURL documentation for possible CURLINFO_* options.

AUTHOR

Tom van der Woerdt <tvdw@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Tom van der Woerdt.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.