Skip to content

guzzlevstreq

MIT 31 13 22,795
448.0 thousand (month) Nov 14 2011 7.8.0(10 months ago)
585 14 56 NOASSERTION
Dec 28 2012 93.2 thousand (month) 23.11.0(8 months ago)

Guzzle is a PHP HTTP client library that makes it easy to send HTTP requests and trivial to integrate with web services. It allows you to send HTTP/1.1 requests with various methods like GET, POST, PUT, DELETE, and others.

Guzzle also supports sending both synchronous and asynchronous requests, caching, and even has built-in support for OAuth 1.0a. Additionally, it can handle different HTTP errors and handle redirects automatically. It also has built-in support for serializing and deserializing data using formats like JSON and XML, as well as sending multipart file uploads.

Overall Guzzle is an easy to use and powerful library for working with HTTP in PHP.

treq is a Python library for making HTTP requests that provides a simple, convenient API for interacting with web services. It is inspired byt the popular requests library, but powered by Twisted asynchronous engine which allows promise based concurrency.

treq provides a simple, high-level API for making HTTP requests, including methods for GET, POST, PUT, DELETE, etc. It also allows for easy handling of JSON data, automatic decompression of gzipped responses, and connection pooling.

treq is a lightweight library and it's easy to use, it's a good choice for small to medium-sized projects where ease of use is more important than performance.

In web scraping treq isn't commonly used as it doesn't support HTTP2 but it's the only Twisted based HTTP client. treq is also based on callback/errback promises (like Scrapy) which can be easier to understand and maintain compared to asyncio's corountines.

Highlights


uses-twistedno-http2

Example Use


use GuzzleHttp\Client;

// Create a client session:
$client = new Client();
// can also set session details like headers
$client = new Client([
    'headers' => [
        'User-Agent' => 'webscraping.fyi',
    ]
]);

// GET request:
$response = $client->get('http://httpbin.org/get');
// print all details
var_dump($response);
// or the important bits
printf("status: %s\n", $response->getStatusCode());
printf("headers: %s\n", json_encode($response->getHeaders(), JSON_PRETTY_PRINT));
printf("body: %s", $response->getBody()->getContents());

// POST request
$response = $client->post(
    'https://httpbin.org/post',
// for JSON use json argument:
    ['json' => ['query' => 'foobar', 'page' => 2]]
// or formdata use form_params:
//  ['form_params' => ['query' => 'foobar', 'page' => 2]]
);

// For ASYNC requests getAsync function can be used:
$promise1 = $client->getAsync('https://httpbin.org/get');
$promise2 = $client->getAsync('https://httpbin.org/get?foo=bar');
// await it:
$results = Promise\unwrap([$promise1, $promise2]);
foreach ($results as $result) {
    echo $result->getBody();
}
// or add promise callback
Promise\each([$promise1, $promise2], function ($response, $index, $callable) {
    echo $response->getBody();
});
from twisted.internet import reactor
from twisted.internet.task import react
from twisted.internet.defer import ensureDeferred
import treq

# treq can be used with twisted's reactor with callbacks
response_deferred = treq.get(
    "http://httpbin.org/get"
)
# or POST
response_deferred = treq.post(
    "http://httpbin.org/post",
    json={"key": "value"},  # JSON
    data={"key": "value"},  # Form Data
)

# add callback or errback
def handle_response(response):
    print(response.code)
    response.text().addCallback(lambda body: print(body))
def handle_error(failure):
    print(failure)
# this callback will be called when request completes:
response_deferred.addCallback(handle_response)
# this errback will be called if request fails
response_deferred.addErrback(handle_error)
# this will be called if request completes or fails:
response_deferred.addBoth(lambda _: reactor.stop())  # close twisted once finished

if __name__ == '__main__':
    reactor.run()

#Note that treq can also be used with async/await:
async def main():
    # content reads response data and get sends a get request:
    print(await treq.content(await treq.get("https://example.com/")))

if __name__ == '__main__':
    react(lambda reactor: ensureDeferred(main()))
```

Alternatives / Similar


Was this page helpful?