Skip to content

guzzlevshttr

MIT 31 13 22,795
448.0 thousand (month) Nov 14 2011 7.8.0(10 months ago)
982 9 2 MIT
May 06 2012 712.9 thousand (month) 1.4.7(1 year, 2 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.

The aim of httr is to provide a wrapper for the curl package, customised to the demands of modern web APIs.

Key features:

  • Functions for the most important http verbs: GET(), HEAD(), PATCH(), PUT(), DELETE() and POST().
  • Automatic connection sharing across requests to the same website (by default, curl handles are managed automatically), cookies are maintained across requests, and a up-to-date root-level SSL certificate store is used.
  • Requests return a standard reponse object that captures the http status line, headers and body, along with other useful information.
  • Response content is available with content() as a raw vector (as = "raw"), a character vector (as = "text"), or parsed into an R object (as = "parsed"), currently for html, xml, json, png and jpeg.
  • You can convert http errors into R errors with stop_for_status().
  • Config functions make it easier to modify the request in common ways: set_cookies(), add_headers(), authenticate(), use_proxy(), verbose(), timeout(), content_type(), accept(), progress().
  • Support for OAuth 1.0 and 2.0 with oauth1.0_token() and oauth2.0_token(). The demo directory has eight OAuth demos: four for 1.0 (twitter, vimeo, withings and yahoo) and four for 2.0 (facebook, github, google, linkedin). OAuth credentials are automatically cached within a project.

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();
});
library(httr)

# GET requests:
resp <- GET("http://httpbin.org/get")
status_code(resp)  # status code
headers(resp)  # headers
str(content(resp))  # body

# POST requests: 
# Form encoded
resp <- POST(url, body = body, encode = "form")
# Multipart encoded
resp <- POST(url, body = body, encode = "multipart")
# JSON encoded
resp <- POST(url, body = body, encode = "json")

# setting cookies:
resp <- GET("http://httpbin.org/cookies", set_cookies("MeWant" = "cookies"))
content(r)$cookies  # get response cookies

Alternatives / Similar


Was this page helpful?