\AlphredRequest

Generic, light-weight, low-functionality wrapper around PHP's cURL library

This Requests library should be good enough for most requests, as long as you aren't doing anything special or crazy. If you outgrow it, then you should either (1) use Guzzle, or (2) write your own requests library that has better coverage.

Granted, this should handle MOST use cases. I don't know if it handles file uploads. Theoretically, it does, but I wouldn't bank on it, and, if it doesn't, I will not expand the functionality to cover file uploads.

With this, you can easily make GET or POST requests. Set extra headers. Easily set a user-agent. Set parameters. And cache the data for later retrieval.

Summary

Methods
Properties
Constants
__construct()
execute()
set_auth()
set_url()
set_user_agent()
set_headers()
add_header()
use_post()
add_parameter()
add_parameters()
clear_cache()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
set_caches()
build_post_fields()
build_get_fields()
build_fields()
get_cached_data()
get_cached_data_anyway()
save_cache_data()
get_cache_key()
get_cache_file()
get_cache_dir()
create_cache_dir()
get_cache_age()
clear_directory()
$handler
$object
N/A

Properties

$handler

$handler : Resource

The internal cURL handler

Type

Resource

$object

$object : array

An internal structuring of the request object for cache creation

Type

array

Methods

__construct()

__construct(string  $url, array  $options = array('cache' => true, 'cache_ttl' => 600, 'cache_bin' => true)) 

Creates a request object

Currently, all the options apply to caching. So the three that are understood are:

  1. cache,
  2. cache_life, and
  3. cache_bin.

cache is a boolean that can turn on/off caching. It is recommended that you turn it on. cache_life is how long the cache will live. In other words, no attempts to get new data will be made until the data saved is older than the cache life (in seconds). It defaults to 3600 (one hour). cache_bin is the sub-directory in the workflow's cache folder where the results are saved. If cache_bin is set to false while caching is turned on, then all the results will be saved directly into the workflow's cache directory.

Cache files are saved as md5 hashes of the request object. So, if you change anything about the request, then it will be considered a new cache file. Data is saved to the cache only if we receive an HTTP response code less than 400.

My advice is not to touch these options and let the cache work with its default behavior.

A further note on cache_bin: the 'cache_bin' option, if true, will create a cache_bin that is a directory in the cache directory named after the hostname. So if the url is http://api.github.com/api.... then the cache_bin will be api.github.com, and all cached data will be saved in that directory. Otherwise, if you pass a string, then that will become the directory it will be saved under.

Parameters

string $url

the URL to request

array $options

[description]

execute()

execute(boolean  $code = false) : string|array

Executes the cURL request

If you set $code to true, then this function will return an associative array as:

[ 'code' => HTTP_RESPONSE_CODE,
  'data' => RESPONSE_DATA
];

If you get cached data, then the code will be "faked" as a 302, which is appropriate.

If there is an error, then the code will be 0. So, if you manage to get expired cache data, then the code will be 0 and there will be data. If there is no expired cache data, then you will receive an array of [ 0, false ].

This method does not cache data unless the response code is less than 400. If you need better data integrity than that, use Guzzle or write your own request library. Or improve this one by putting in a pull request on the Github repo.

Parameters

boolean $code

whether or not to return an HTTP response code

Returns

string|array —

the response data, or an array with the code

set_auth()

set_auth(string  $username, string  $password) 

Sets basic authorization for a cURL request

If you need more advanced authorization methods, and if you cannot make them happen with headers, then use a different library. I recommend Guzzle.

Parameters

string $username

a username

string $password

a password

set_url()

set_url(string  $url) 

Sets the URL for the cURL request

Parameters

string $url

a valid URL

Throws

\Alphred\Exception

when $url is not a valid URL

set_user_agent()

set_user_agent(string  $agent) 

Sets the `user agent` for the cURL request

Parameters

string $agent

a user agent

set_headers()

set_headers(string|array  $headers) 

Sets the headers on a cURL request

Parameters

string|array $headers

sets extra headers for the cURL request

add_header()

add_header(string|array  $header) 

Adds a header into the headers array

You can actually pass multiple headers with an array, or just pass a single header with a string.

Parameters

string|array $header

the header to add

use_post()

use_post() 

Sets the request to use `POST` rather than `GET`

add_parameter()

add_parameter(string  $key, string  $value) 

Adds a parameter to the parameters array

Parameters

string $key

the name of the parameter

string $value

the value of the parameter

add_parameters()

add_parameters(array  $params) 

Adds parameters to the request

Parameters

array $params

an array of parameters

Throws

\Alphred\Exception

when passed something other than an array

clear_cache()

clear_cache(string|boolean  $bin = false) : null

Clears a cache bin

Call the file with no arguments if you aren't using a cache bin; however, this will choke on sub-directories.

Parameters

string|boolean $bin

the name of the cache bin (or a URL if you're setting them automatically)

Throws

\Alphred\Exception

when encountering a sub-directory

Returns

null

set_caches()

set_caches(array  $options) 

Sets the cache options

Parameters

array $options

an array of cache options

build_post_fields()

build_post_fields() 

Builds the post fields array

build_get_fields()

build_get_fields() 

Builds the post fields array

build_fields()

build_fields() 

Builds the fields out of parameters array

get_cached_data()

get_cached_data(  $ignore_life = false) : string|boolean

Gets cached data

This method first checks if the cache file exists. If $ignore_life is true, then it will return the data without checking the life. Otherwise, we'll check to make sure that the $cache_life is set. Next, we check the age of the cache. If any of these fail, then we return false, which indicates we should get new data. Otherwise, we retrieve the cache.

Parameters

$ignore_life

Returns

string|boolean —

the data saved in the cache or false

get_cached_data_anyway()

get_cached_data_anyway() : string|boolean

Retrieves cached data regardless of cache life

Returns

string|boolean —

returns the cached data or false if none exists

save_cache_data()

save_cache_data(string  $data) 

Saves data to a cache file

Parameters

string $data

the data to save to the cache

get_cache_key()

get_cache_key() : string

Creates a cache key based on the request object

Returns

string —

a cache key

get_cache_file()

get_cache_file() : string

Returns the file cache

Returns

string —

the full path to the cache file

get_cache_dir()

get_cache_dir() : string

Returns the directory for the cache

Returns

string —

full path to cache directory

create_cache_dir()

create_cache_dir() : boolean

Creates a cache directory if it does not exist

Throws

\Alphred\RunningOutsideOfAlfred

when environmental variables are not set

Returns

boolean —

success or failure if directory has been made

get_cache_age()

get_cache_age() : integer

Gets the age of a cache file

Returns

integer —

the age of a file in seconds

clear_directory()

clear_directory(string  $dir) 

Clears all the files out of a directory

Parameters

string $dir

a path to a directory

Throws

\Alphred\Exception

when encountering a sub-directory