Skip to content
jrbasso edited this page Dec 15, 2010 · 11 revisions

The HttpSocket class has undergone some changes.

Response are now an object

The get(), post(), put(), delete() and request() methods now returns the response as object. This object receive the raw content from remote and it is responsible to manipulate the data. CakePHP comes with HttpResponse to manipulate the response as CakePHP 1.3. This class implements the ArrayAccess and __toString(), so you can continue using the $http->response as array and the return of request methods as string. But, will be more fast if you use it as object:

$response = $http->get('http://www.cakephp.org');

// From:
$titlePos = strpos($response, '<title>');
$code = $http->response['status-line]['code'];

// To:
$titlePos = strpos($response->body, '<title>');
$code = $response->code;

This new class have the below attributes:

  • body: body of HTTP (normally the HTML). Old response of method or $http->request['body'];
  • headers: array with headers. It replace $http->request['header'];
  • cookies: array with new cookies (cookies from others request are not stored here). It replace $http->request['cookies'];
  • httpVersion: string with HTTP version (from first line in response). It replace `$http->request['status']['http-version'];
  • code: integer with HTTP code. It replace $http->request['status']['code'];
  • reasonPhrase: string with HTTP code response. It replace $http->request['status']['reason-phrase'];
  • raw: unchanged response from server. It replace $http->request['raw']['response'].

And the new methods:

  • body(): return the body;
  • isOk(): if code is 200;
  • getHeader(): will be explained in the next session.

You can create your own response class to use with HttpSocket. You could create the file `app/libs/your_response.php' with the content:

<?php

App::import('Lib', 'HttpResponse');

class YourResponse extends HttpResponse {

    public function parseResponse($message) {
        parent::parseResponse($message);
        // Make what you want
    }
}

Before your request you need change $http->responseClass to YourResponse.

HttpSocket doesn't change the header keys

Following others places in core, the HttpSocket do not change the headers. RFC 2616 says that headers are case insensitive, and HttpSocket preserves the values the remote host sends.

So, if your server sends the headers like

HTTP/1.1 200 OK
Date: Mon, 16 Apr 2007 04:14:16 GMT
server: CakeHttp Server
content-tyPe: text/html

Your $response->headers (or $response['header']) will contain the exact keys sent.

array(
    'Date' => 'Mon, 16 Apr 2007 04:14:16 GMT',
    'server' => 'CakeHttp Server',
    'content-tyPe' => 'text/html'
)

To get a safe header, you can use the method HttpResponse::getHeader($name) that will get the header in insensitive case to you. For example, $response->getHeader('dAtE') will return Mon, 16 Apr 2007 04:14:16 GMT.

Getting result using a stream (or simply download the result)

HttpSocket has a new method called setContentResource(). By setting a resource with this method, the content will be written to this resource, using fwrite(). To you download a file, you can do:

$http = new HttpSocket();
$f = fopen('bakery.xml', 'w');
$http->setContentResource($f);
$http->get('http://bakery.cakephp.org/comments.rss');
fclose($f);

Attention: The header is not included in file, you will only get the body content written to your resource. To disable saving into the resource, use $http->setContentResource(false).

Independent cookie by host

Cookies are stored internally by host, not per instance. This means that, if you make two requests to different servers, cookies from domain1 won't be sent to domain2. This was done to avoid possible security problems.

Extensible Authentication methods

You can now create your own authentication method to use with HttpSocket. You could create the file `app/libs/http/your_method_authentication.php' with the content:

<?php

class YourMethodAuthentication {

/**
 * Authentication
 *
 * @param HttpSocket $http
 * @param array $authInfo
 * @return void
 */
    public static function authentication(HttpSocket $http, &$authInfo) {
        // Do something, for example set $http->request['header']['Authentication'] value
    }

}

To set the auth config, you can use the new method configAuth():

$http->configAuth('YourMethod', array('config1' => 'value1', 'config2' => 'value2'));
$http->get('http://secure.your-site.com');

The Basic authentication is still supported by CakePHP, support for Digest authentication has also been added. To use this feature you will need to set $http->configAuth('Basic', 'user', 'password');

Support to Proxy

As part of auth configuration, you can configure proxy authentication. You can create your customized method to proxy authentication in the same class of authentication. For example:

<?php

class YourMethodAuthentication {

/**
 * Authentication
 *
 * @param HttpSocket $http
 * @param array $authInfo
 * @return void
 */
    public static function authentication(HttpSocket $http, &$authInfo) {
        // Do something, for example set $http->request['header']['Authentication'] value
    }

/**
 * Proxy Authentication
 *
 * @param HttpSocket $http
 * @param array $proxyInfo
 * @return void
 */
    public static function proxyAuthentication(HttpSocket $http, &$proxyInfo) {
        // Do something, for example set $http->request['header']['Proxy-Authentication'] value
    }

}

To use, you must call the HttpSocket::configProxy() as same the HttpSocket::configAuth().

Clone this wiki locally