Constants

VERSION

VERSION

The version of this lib

DEFAULT_TIMEOUT

DEFAULT_TIMEOUT

the default timeout for requests, in seconds

DEFAULT_MAX_REDIRECTS

DEFAULT_MAX_REDIRECTS

the default maximum number of redirects

Properties

$JSON_TYPES

$JSON_TYPES : array

content-types that will trigger JSON parsing of body

Type

array

$XML_TYPES

$XML_TYPES : array

content-types that will trigger XML parsing

Type

array

$debug

$debug : bool

Type

bool — enables debugging output

$logger

$logger : \Closure

logging function (should be a Closure)

Type

\Closure

$parse_body

$parse_body : bool

Type

bool — whether or not to auto-parse the response body as JSON or XML

$user_agent

$user_agent : string

Type

string — The user agent sent with the request

$base_url

$base_url : string

Type

string — the base url used to construct request endpoints

$last_request

$last_request : array

Stores the last request hash

Type

array

$last_response

$last_response : array

Stores the last response hash

Type

array

$callbacks

$callbacks : array

stores anon func callbacks (because you can't store them as obj props

Type

array

$username

$username : string

username for basic auth

Type

string

$password

$password : string

password for basic auth

Type

string

$silence_fopen_warning

$silence_fopen_warning : boolean

by default, silence the fopen warning if we can't open the stream

Type

boolean

$raise_fopen_exception

$raise_fopen_exception : boolean

by default, don't raise an exception if fopen() fails

Type

boolean

$supports_patch

$supports_patch : boolean

if true, send X-HTTP-Method-Override header for PATCH requests. default is false

Type

boolean

$json_to_array

$json_to_array : boolean

by default, convert decoded JSON to an object of StdClass. If true, convert to an array.

Type

boolean

Methods

__construct()

__construct(array $opts)

Class constructor

Passed opts can include:

  • $opts['onRequestLog'] - an anonymous function that takes the Resty::last_request property as arg
  • $opts['onResponseLog'] - an anonymous function that takes the Resty::last_response property as arg
  • $opts['silence_fopen_warning'] - boolean: silence warnings from fopen when trying to open stream
  • $opts['raise_fopen_exception'] - boolean: raise an exception from fopen if trying to open stream fails
  • $opts['supports_patch'] - boolean: set to false if the REST end point you're connecting to does not support PATCH -- will use the X-HTTP-Method-Override header.
  • $opts['json_to_array'] - boolean: set to true if decoded JSON should be an array, not an object

Parameters

array $opts

OPTIONAL array of options

getLastRequest()

getLastRequest(string $key) : mixed

retrieve the last request we sent

valid keys are ['url', 'method', 'querydata', 'headers', 'options', 'opts']

Parameters

string $key

just retrieve a given field from the hash

Returns

mixed

getLastResponse()

getLastResponse(string $key) : mixed

retrieve the last response we got

valid keys are ['meta', 'status', 'headers', 'body']

Parameters

string $key

just retrieve a given field from the hash

Returns

mixed

getLink()

getLink(string $rel) : \Resty\stdClass

get a specific link (via the rel tag) from the last response

Parameters

string $rel

Returns

\Resty\stdClass —

with a link and type.

get()

get(string $url, array|string $querydata, array $headers, array $options) : array

make a GET request

Parameters

string $url

the URL. This will be appended to the base_url, if any set

array|string $querydata

hash of key/val pairs. This will be appended to the URL

array $headers

hash of key/val pairs

array $options

hash of key/val pairs ('timeout')

Returns

array —

the response hash

post()

post(string $url, array|string $querydata, array $headers, array $options) : array

make a POST request

Parameters

string $url

the URL. This will be appended to the base_url, if any set

array|string $querydata

hash of key/val pairs. This will be sent in the request body

array $headers

hash of key/val pairs

array $options

hash of key/val pairs ('timeout')

Returns

array —

the response hash

put()

put(string $url, array|string $querydata, array $headers, array $options) : array

make a PUT request

Parameters

string $url

the URL. This will be appended to the base_url, if any set

array|string $querydata

hash of key/val pairs. This will be sent in the request body

array $headers

hash of key/val pairs

array $options

hash of key/val pairs ('timeout')

Returns

array —

the response hash

patch()

patch(string $url, array|string $querydata, array $headers, array $options) : array

make a PATCH request

Parameters

string $url

the URL. This will be appended to the base_url, if any set

array|string $querydata

hash of key/val pairs. This will be sent in the request body

array $headers

hash of key/val pairs

array $options

hash of key/val pairs ('timeout')

Returns

array —

the response hash

delete()

delete(string $url, array|string $querydata, array $headers, array $options) : array

make a DELETE request

Parameters

string $url

the URL. This will be appended to the base_url, if any set

array|string $querydata

hash of key/val pairs. This will be appended to the URL

array $headers

hash of key/val pairs

array $options

hash of key/val pairs ('timeout')

Returns

array —

the response hash

postJson()

postJson(string $url, array|\Resty\stdClass $structure, array $headers, array $options) : array

make a POST request with a JSON body.

The Content-Type header will be set to 'application/json'

Parameters

string $url

the URL. This will be appended to the base_url, if any set

array|\Resty\stdClass $structure

the array or object to send as JSON

array $headers

hash of key/val pairs

array $options

hash of key/val pairs ('timeout')

Returns

array —

the response hash

putJson()

putJson(string $url, array|\Resty\stdClass $structure, array $headers, array $options) : array

make a PUT request with a JSON body.

The Content-Type header will be set to 'application/json'

Parameters

string $url

the URL. This will be appended to the base_url, if any set

array|\Resty\stdClass $structure

the array or object to send as JSON

array $headers

hash of key/val pairs

array $options

hash of key/val pairs ('timeout')

Returns

array —

the response hash

patchJson()

patchJson(string $url, array|\Resty\stdClass $structure, array $headers, array $options) : array

make a PATCH request with a JSON body.

The Content-Type header will be set to 'application/json'

Parameters

string $url

the URL. This will be appended to the base_url, if any set

array|\Resty\stdClass $structure

the array or object to send as JSON

array $headers

hash of key/val pairs

array $options

hash of key/val pairs ('timeout')

Returns

array —

the response hash

postFiles()

postFiles(string $url, array $files, array $params, array $headers, array $options) : array

POST one or more files

The $files array should be a set of key/val pairs, with the key being the field name, and the val the file path. ex: $files['avatar'] = '/path/to/file.jpg'; $files['background'] = '/path/to/file2.jpg';

Parameters

string $url
array $files
array $params
array $headers
array $options

Returns

array

postBinary()

postBinary(string $url, array $binary_data, array $params, array $headers, array $options) : array

POST binary data directly, without reading from file(s)

The $binary_data array should be a set of key/val pairs, with the key being the field name, and the val the binary data. ex: $files['avatar'] = ; $files['background'] = ;

with that data, a multipart POST body is created, identical to a file upload, just without reading the data from a file

Parameters

string $url
array $binary_data
array $params
array $headers
array $options

Returns

array

enableDebugging()

enableDebugging(bool $state)

bc wrapper

Parameters

bool $state

debug()

debug(bool $state) : bool

enable or disable debugging. If no arg passed, just returns current state

Parameters

bool $state

if not passed, state not changed

Returns

bool —

the current state

raiseFopenException()

raiseFopenException(boolean $state) : boolean

raise an exception from fopen if trying to open stream fails

Parameters

boolean $state

= null optional, set the state

Returns

boolean —

the current state

silenceFopenWarning()

silenceFopenWarning(boolean $state) : boolean

silence warnings from fopen when trying to open stream

Parameters

boolean $state

optional, set the state

Returns

boolean —

the current state

supportsPatch()

supportsPatch(boolean $state) : boolean

configure whether or not the REST endpoint supports the HTTP PATCH method. If set to false, the X-HTTP-Method-Override header will be sent using HTTP POST.

Parameters

boolean $state

= null optional, set the state

Returns

boolean —

the current state

jsonToArray()

jsonToArray(boolean $state) : boolean

configure whether to decode JSON into an array or an object. By default, converts to object.

Parameters

boolean $state

= null optional, set the state

Returns

boolean —

the current state

setLogger()

setLogger(\Resty\Closure $logger)

sets an alternate logging method

Parameters

\Resty\Closure $logger

parseBody()

parseBody(bool $state)

enable or disable automatic parsing of body. default is true

Parameters

bool $state

default TRUE

setBaseURL()

setBaseURL(string $base_url)

Sets the base URL for all subsequent requests

Parameters

string $base_url

getBaseURL()

getBaseURL() : string

retrieves the current Resty::$base_url

Returns

string

setUserAgent()

setUserAgent(string $user_agent)

Sets the user-agent

Parameters

string $user_agent

getUserAgent()

getUserAgent() : string

Gets the current user agent. if Resty::$user_agent is not set, uses a default

Returns

string

setCredentials()

setCredentials(string $username, string $password)

Sets credentials for http basic auth

Parameters

string $username
string $password

clearCredentials()

clearCredentials()

removes current credentials

sendRequest()

sendRequest(string $url, string $method, array|string $querydata, array $headers, array $options) : array

Sends the HTTP request and retrieves/parses the response

Parameters

string $url
string $method
array|string $querydata

OPTIONAL

array $headers

OPTIONAL

array $options

OPTIONAL

Returns

array

sendJsonRequest()

sendJsonRequest(string $url, string $method, array|\Resty\stdClass $structure, array $headers, array $options) : array

A wrapper for Resty::sendRequest that encodes the $structure as JSON and sets the Content-Type header will be set to 'application/json'

Parameters

string $url

the URL. This will be appended to the base_url, if any set

string $method

the HTTP method. This should really only be (POST|PUT|PATCH)

array|\Resty\stdClass $structure

the array or object to send as JSON

array $headers

hash of key/val pairs

array $options

hash of key/val pairs ('timeout')

Returns

array —

the response hash

getMimeType()

getMimeType(string $filepath)

Stole this from the Amazon S3 class:

Copyright (c) 2008, Donovan Schönknecht. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Amazon S3 is a trademark of Amazon.com, Inc. or its affiliates.

Parameters

string $filepath

the path to the file

buildHeadersArray()

buildHeadersArray(string $headers) : void

takes a set of key/val pairs and builds an array of raw header strings

Parameters

string $headers

metaToHeaders()

metaToHeaders(array $meta) : array

Extracts the headers of a response from the stream's meta data

Parameters

array $meta

Returns

array

getStatusCode()

getStatusCode(array $meta) : integer

extracts the status code from the stream meta data

Parameters

array $meta

Returns

integer

makeStreamRequest()

makeStreamRequest(string $url, array $opts) : array

opens an http stream, sends the request, and returns result

Parameters

string $url
array $opts

Throws

\Resty\Exception

If opening stream fails.

Returns

array

processResponseBody()

processResponseBody(string $resp) : string|object

If we get back something that claims to be XML or JSON, parse it as such and assign to $resp['body']

Parameters

string $resp

Returns

string|object

calcTimePassed()

calcTimePassed(float $start_time) : float

calculate time passed in microtime

Parameters

float $start_time

should be result of microtime(true)

Returns

float —

the diff between passed microtime and current microtime

log()

log(mixed $msg) : array

The wrapper method for logging. Either calls a custom logger or Resty::defaultLogger()

Parameters

mixed $msg

Returns

array

defaultLogger()

defaultLogger(mixed $msg) : array

logging helper

Parameters

mixed $msg

Returns

array