Code Monkey home page Code Monkey logo

bget's Introduction

Better Getter

© 2011 Garrett Albright & GoingOn, Ltd

Better Getter (Bget) is an award-winning PHP library which wraps around PHP's built-in cURL library and makes it less painful to use. It's completely object-oriented and designed to be crazy extensible.

Simple examples

Using PHP's cURL library alone to fetch a file at a URI:

<?php
// Initialize the cURL handle
$ch = curl_init('http://example.com/');
// Tell cURL we want the response to be returned
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Execute the handle and get the response
$response = curl_exec($ch);
?>

Using Better Getter:

<?php
// Initialize a Better Getter (Bget) instance
$bget = new Bget('http://example.com/');
// Execute it and get the response. Note the use of chaining.
$response = $bget->execute()->getRawResponse();
?>

Not yet convinced? Here's something a little more complex; getting the value of the "Date" HTTP header from a response. The standard cURL library doesn't have much in the way of helping you with getting HTTP headers from a response, so you're mostly on your own.

<?php
// Initialize the cURL handle
$ch = curl_init('http://example.com/');
// Tell cURL we want the response to be returned
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// …And to include HTTP headers with the response
curl_setopt($ch, CURLOPT_HEADER, TRUE);
// Execute the handle and get the response
$response = curl_exec($ch);
// Split the headers away from the body of the response so we don't
// accidentally find stuff that's in the body. Also, this is a rather
// brittle way of going about this since cURL sometimes returns several sets
// of headers when doing authentication, redirection, etc. But for
// simplicity's sake:
list($headers, $body) = explode("/r/n/r/n", $response, 2);
// Now see if the header we're looking for is in the headers string
if (preg_match('/^Date: (.*)/', $headers, $matches)) {
  $date_header = $matches[1];
}
?>

But Better Getter is designed to be extensible, and as an example of that, it comes with the BgetHttp class, a child of the standard Bget class which adds many nice functions for working with HTTP things, like headers. We can do this:

<?php
// Initialize a Better Getter HTTP instance
$bget = new BgetHttp('http://example.com/');
// Execute it
$bget->execute();
// Get the value of the "Date" HTTP header
$date_header = $bget->getResponseHeader('Date');
?>

Or, if you really love chaining…

<?php
$bget = new BgetHttp('http://example.com/');
$date_header = $bget->execute()->getResponseHeader('Date');
?>

Not using HTTP? Better Getter can be used for FTP, Gopher, or anything else that cURL itself can access. If Better Getter (or Better Getter HTTP) doesn't support what you need to do, you can set cURL options yourself using the setCurlOpt() method; however, the idea is that you should subclass the Bget (or BgetHttp) class so that your subclass's methods can set those options automatically itself. May Better Getter inspire you!

License

Better Getter is dual-licensed under the MIT and GNU GPL v2 licenses, which should make it appropriate for any project you want to use it under. See the LICENSE.txt file for more information.

Contact

For inquiries, contact Garrett Albright at [email protected]

Like social networking? Like higher education? GoingOn mixes the two like strawberries and bananas.

bget's People

Contributors

twistor avatar

Watchers

 avatar

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.