Git Product home page Git Product logo

fetch's Introduction

Fetch

Fetch is a thin PHP wrapper around PHP's Curl libraries.

Make a Request

Fetch has lots of options you can set and my examples aren't exhaustive, just take a look at the code in Fetch.php to see what options you can set.

To fetch one url (and wait while it downloads)

include("FetchBase.php");
include("Fetch.php");
include("FetchResponse.php");

$f = new Fetch('http://example.com');
$r = $f->get();

echo $r->getCode();
echo $r->getHeaders();
echo $r->getBody();

To fetch multiple urls (while you do something else)

$urls = array(
  'http://example.com',
  'https://example2.com'
);
$fs = array();

foreach($urls as $url){
  $f = newFetch($url);
  $f->start();
  $fs[] = $f;

}//foreach

/* Now do whatever else you want to do, go crazy!!! */

foreach($fs as $f){
  $r = $f->stop();
  echo $r->getCode();
  echo $r->getHeaders();
  echo $r->getBody();

}//foreach

To fetch a file

$path = '/local/path/to/file';

$f = new Fetch('http://example.com/remote/path/to/file');
$f->setPath($path);
$r = $f->get();
$path = $r->getPath();
echo file_exists($path);

You can also download the files in the background:

$file_urls = array(
  'http://example.com/remote/path/to/file',
  'https://example2.com/remote/path/to/file'
);
$fs = array();

foreach($file_urls as $file_url){
  $f = newFetch($file_url);
  $f->start();
  $fs[] = $f;

}//foreach

/* Now do whatever else you want to do while the files download, go crazy!!! */

foreach($fs as $f){
  $r = $f->stop();
  $path = $r->getPath();
  echo file_exists($path);

}//foreach

Why would I use this over Zend Framework's HTTP Request among others?

Eh, you probably wouldn't, truth is I've had this code lying around in some state since about 2005. In that time it has gone through multiple rewrites, of which this is the latest; but since it is already written, I figured I would make it available since it does work and it is pretty bulletproof, and it does do what I want it to do, and it is extremely lightweight, just three files, so it is easy to include in a small project.

License

MIT, pull requests are welcome also.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.