Download the PHP package atk14/url-fetcher without Composer

On this page you can find all versions of the php package atk14/url-fetcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package url-fetcher

UrlFetcher

Tests

UrlFetcher is a PHP class providing methods to make HTTP requests.

Installation

composer require atk14/url-fetcher

Basic usage

$fetcher = new UrlFetcher("http://www.example.com/content.dat");

if(!$fetcher->found()){
  echo $fetcher->getErrorMessage();
  exit(1);
}

$content = $fetcher->getContent(); // returns StringBufferTemporary

$status_code    = $fetcher->getStatusCode();    // e.g. 200
$status_message = $fetcher->getStatusMessage(); // e.g. "OK"
$content_type   = $fetcher->getContentType();   // e.g. "application/pdf"
$charset        = $fetcher->getContentCharset(); // e.g. "UTF-8"
$content_length = $fetcher->getContentLength(); // e.g. "12345"
$filename       = $fetcher->getFilename();      // e.g. "content.dat"

$request_headers  = $fetcher->getRequestHeaders();
$response_headers = $fetcher->getResponseHeaders();

HTTP Basic Authentication

Credentials can be passed directly in the URL:

$fetcher = new UrlFetcher("https://username:[email protected]:444/private/content.dat");
if($fetcher->found()){
  echo $fetcher->getContent();
}

Or set explicitly:

$fetcher = new UrlFetcher("https://www.example.com/private/content.dat");
$fetcher->setAuthorization("username", "password");
if($fetcher->found()){
  echo $fetcher->getContent();
}

$fetcher->resetAuthorization(); // clear credentials

POST request

Array is sent as application/x-www-form-urlencoded:

$fetcher = new UrlFetcher("https://www.example.com/api/articles/");
if($fetcher->post(["title" => "Sample article", "body" => "Lorem Ipsum..."])){
  echo $fetcher->getContent();
}

Raw string body with a custom content type:

$fetcher = new UrlFetcher("https://www.example.com/api/articles/");
$fetcher->post($json, ["content_type" => "application/json"]);

PUT and DELETE requests

$fetcher = new UrlFetcher("https://www.example.com/api/articles/123/");
$fetcher->put($json, ["content_type" => "application/json"]);

$fetcher = new UrlFetcher("https://www.example.com/api/articles/123/");
$fetcher->delete();

Custom headers

Headers passed to the constructor are sent with every request made by the instance:

$fetcher = new UrlFetcher("https://www.example.com/", [
  "additional_headers" => ["X-App-Version: 1.2", "Accept: application/json"]
]);

Headers passed to post(), put(), delete() or fetchContent() are sent only with that single request:

$fetcher->post($data, ["additional_headers" => ["X-Request-ID: abc123"]]);

Inspecting response headers

// single header value (case-insensitive)
$type = $fetcher->getHeaderValue("Content-Type"); // "text/html; charset=UTF-8"

// all headers as an associative array
$headers = $fetcher->getResponseHeaders(["as_hash" => true, "lowerize_keys" => true]);
echo $headers["content-type"];

Redirections

Redirects (301, 302, 303) are followed automatically. The default limit is 5. It can be changed in the constructor:

$fetcher = new UrlFetcher("http://www.example.com/", ["max_redirections" => 10]);

// disable redirections entirely
$fetcher = new UrlFetcher("http://www.example.com/", ["max_redirections" => 0]);

After a redirect, getUrl() returns the final URL:

$fetcher = new UrlFetcher("http://www.example.com/old-page/");
$fetcher->getContent();
echo $fetcher->getUrl(); // the final URL after redirection

SSL / HTTPS

SSL peer verification is enabled by default. It can be disabled for self-signed or expired certificates:

$fetcher = new UrlFetcher("https://www.example.com/", [
  "verify_peer"      => false,
  "verify_peer_name" => false,
]);

The default can also be changed globally via a PHP constant before including the library:

define("URL_FETCHER_VERIFY_PEER", false);

Proxy

$fetcher = new UrlFetcher("http://www.example.com/", [
  "proxy" => "tcp://127.0.0.1:8118"
]);

Connecting to a specific IP address

By default, the socket is connected to the hostname resolved from the URL. To bypass DNS resolution and connect directly to a given IP address, use the ip_address option. The Host header (and, for HTTPS, SNI / certificate verification) still uses the original hostname:

$fetcher = new UrlFetcher("https://www.example.com/", [
  "ip_address" => "192.0.2.1"
]);

This option has no effect when a proxy is configured.

Timeouts

Two separate timeouts can be configured:

They can also be changed at any time. Both methods return the previous value:

$prev = $fetcher->setSocketTimeout(3.0);
$prev = $fetcher->setReadTimeout(10.0);

User-Agent

$fetcher = new UrlFetcher("http://www.example.com/", [
  "user_agent" => "MyApp/2.0"
]);

HTTP version

By default, requests are sent as HTTP/1.0. To use HTTP/1.1 instead, set the http_version option:

$fetcher = new UrlFetcher("http://www.example.com/", [
  "http_version" => "1.1"
]);

UrlFetcherViaCommand

UrlFetcherViaCommand is a subclass that sends the HTTP request to an external command via stdin and reads the response from stdout. This is useful for testing or simulating HTTP requests inside an application (e.g. ATK14's scripts/simulate_http_request).

$fetcher = new UrlFetcherViaCommand("nc localhost 80", "http://localhost/content.dat");
if($fetcher->found()){
  echo $fetcher->getContent();
}

Licence

UrlFetcher is free software distributed under the terms of the MIT license


All versions of url-fetcher with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6
atk14/string-buffer Version >=1.2.2 <2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package atk14/url-fetcher contains the following files

Loading the files please wait ...