httpfulvshttr
Httpful is a simple Http Client library for PHP 7.2+. There is an emphasis of readability, simplicity, and flexibility – basically provide the features and flexibility to get the job done and make those features really easy to use.
Features
- Readable HTTP Method Support (GET, PUT, POST, DELETE, HEAD, PATCH and OPTIONS)
- Custom Headers
- Automatic "Smart" Parsing
- Automatic Payload Serialization
- Basic Auth
- Client Side Certificate Auth
- Request "Templates"
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
require 'vendor/autoload.php';
use Httpful\Request;
// make GET request
$response = \Httpful\Request::get("http://httpbin.org/get")
->send();
echo $response->body;
// make POST request
$data = array('name' => 'Bob', 'age' => 35);
$response = \Httpful\Request::post("http://httpbin.org/post")
->sendsJson()
->body(json_encode($data))
->send();
echo $response->body;
// add headers or cookies
$response = \Httpful\Request::get("http://httpbin.org/headers")
->addHeader("API-KEY", "mykey")
->addHeader("Cookie", "foo=bar")
->send();
echo $response->body;
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