Skip to content

guzzlevsfaraday

MIT 31 13 22,795
448.0 thousand (month) Nov 14 2011 7.8.0(10 months ago)
5,702 7 39 MIT
Dec 19 2009 4.6 million (month) 2.9.2(27 days 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.

Faraday is a Ruby gem that provides a simple and flexible interface for making HTTP requests. It allows you to create a Faraday connection object, which you can use to send requests and receive responses.

Faraday abstracts away the details of the underlying HTTP client library, so you can use it with different libraries such as Net::HTTP, HTTPClient, typhoeus and others.

Since Faraday can adapt many other HTTP clients it's very popular choice in web scraping.

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();
});
# GET requests
response = Faraday.get('http://httpbingo.org')
put response.status
put response.headers
put response.body

# or use a persistent client session:
conn = Faraday.new(
  url: 'http://httpbin.org/get',
  params: {param: '1'},
  headers: {'Content-Type' => 'application/json'}
)

# POST requests
response = conn.post('/post') do |req|
  req.params['limit'] = 100
  req.body = {query: 'chunky bacon'}.to_json
end

Alternatives / Similar


Was this page helpful?