Skip to content

node-fetchvsguzzle

MIT 207 10 8,709
224.9 million (month) Dec 28 2012 3.3.2(7 months ago)
22,795 13 31 MIT
Nov 14 2011 448.0 thousand (month) 7.8.0(10 months ago)

node-fetch is a lightweight library that provides a fetch()-like API for making HTTP requests in Node.js. It is a light-weight implementation of the Fetch API, which is mostly compatible with the browser's version.

node-fetch is primarily known as almost identical package fetch() is included in web browsers so it shares the same use common API. It's great starting point for people coming from front-end environment.

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.

Highlights


popular

Example Use


const fetch = require('node-fetch');

// fetch supports both Promises and async/await
fetch('http://httpbin.org/get')
    .then(res => res.text())
    .then(body => console.log(body))
    .catch(err => console.error(err));
const response = await fetch('http://httpbin.org/get');

// for concurrent scraping Promise.all can be used
const results = await Promise.all([
  fetch('http://httpbin.org/html'),
  fetch('http://httpbin.org/html'),
  fetch('http://httpbin.org/html'),
])

// POST requests
await fetch('http://httpbin.org/post', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name: 'John Doe' }),
})

// Proxy use:
const agent = new https.Agent({
    rejectUnauthorized: false,
    proxy: {
        host: 'proxy.example.com',
        port: 8080
    }
});

await fetch('https://httpbin.org/ip', { agent })

// setting headers and cookies
const headers = new fetch.Headers();
headers.append('Cookie', 'myCookie=123');
headers.append('X-My-Header', 'myValue');

await fetch('https://httpbin.org/headers', { headers })
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();
});

Alternatives / Similar


Was this page helpful?