waNet

Sending requests to remote resources

Contents...

This class is useful for convenient sending of network requests using curl extension, and via file_get_contents and fsockopen functions. The class automatically uses the first of the sending methods (transports) available on the server, verifies the received response, and returns its contents, unpacked to the required format, if necessary.

How to use waNet class:

  1. Create a class instance by passing an array of initializing parameters to the constructor.
  2. If necessary, call "preparation" methods: cookies and userAgent.
  3. To actually send a request, call method query, which will return the response contents.
  4. If necessary, additionally call other methods returning more information about the received response: getResponse и getResponseHeader.

Methods

public function __construct ($options)

Parameters

Example

$options = array(
    'format'        => waNet::FORMAT_JSON
    'timeout'       => 10,
    'authorization' => true,
    'login'         => $login,
    'password'      => $password,
);
$net = new waNet($options);

public function cookies ($path)

Sets the path to a file with cookies values for using as the CURLOPT_COOKIEFILE parameter value when connecting via curl. This default value is used, only if no other path to a cookies file is explicitly specified in curl connection parameters.

Parameters

Example

$net = new waNet($options);
$net->cookies($path);

public function getResponse ($raw = false)

Returns the contents of the last received response.

Parameters

Example

$options['format'] = waNet::FORMAT_JSON;
$net = new waNet($options);
$decoded_response = $net->query($url);
$raw_response = $net->getResponse(true);

public function getResponseHeader ($header = null)

Returns the contents of the last received response headers.

Parameters

Example

$net = new waNet($options);
$response = $net->query($url);
$response_headers = $net->getResponseHeader();

public function query ($url, $content = array(), $method = self::METHOD_GET)

Sends a request to a remote resource and returns response contents formatted according to the specified format name.

Execution of this method may cause an exception; therefore, it must be used inside a try...catch block for correct error handling. Error codes in this case correspond to common HTTP server response codes.

Parameters

Example

$net = new waNet($options);
$response = $net->query($url, $content, waNet::METHOD_POST);

public function userAgent ($user_agent = null)

Sets a new or returns the current value of the "User-Agent" header.

Parameters

Example

$net = new waNet($options);
$net->userAgent($custom_user_agent);
$response = $net->query($url);