Skip to content
semsol edited this page Mar 11, 2011 · 2 revisions

ARC's HTTP Reader can be used as a stand-alone component for generic GET or POST operations. It auto-follows redirects, supports Basic and Digest Authentication, and can be used with TLS.

Instantiation

$conf = array(
  ...
);
$reader = ARC2::getComponent('Reader', $conf);

Establishing an HTTP connection

$url = 'http://example.com/';
$reader->activate($url);

Reading from a local file

$path = '../data/test.rdf'; // absolute paths work, too
$reader->activate($path);

Format detection

The activate() method has to be called already.

$format = $reader->getFormat(); // e.g. "html", "turtle", "rdfxml", etc. 

Streaming data retrieval

$doc = '';
while ($buffer = $reader->readStream()) {
  $doc .= $buffer;
}

Closing the connection

$reader->closeStream();

Connection errors

The Reader uses ARC's normal error hooks.

if ($errs = $reader->getErrors()) {
  print_r($errs);
}

Retrieving the response headers (if any)

$headers = $reader->getResponseHeaders();

Retrieving the list of redirects (if any)

$redirs = $reader->getRedirects();

POST operations

$reader->setHTTPMethod('POST');
$reader->setMessageBody('a=foo&b=bar');
$reader->activate($url);

Custom request headers

This is very low-level. Make sure your header code is valid.

// override all custom headers
$reader->setCustomHeaders("Accept: x-turtle; q=0.98");
// append header code
$reader->addCustomHeaders("x-test1: foo\r\nx-test2: bar")

Basic Auth configuration

Use a (single) colon to separate the user name from the password.

$conf = array(
  'arc_reader_credentials' => array(
     'twitter.com' => 'user:pass',
  )
);
$reader = ARC2::getComponent('Reader', $conf);

Digest Auth configuration

Use 2 colons to separate the user name from the password.

$conf = array(
  'arc_reader_credentials' => array(
     'api.talis.com/stores/johndoe/meta' => 'johndoe::pass',
  )
);
$reader = ARC2::getComponent('Reader', $conf);

SSL configuration options

All configuration options that start with "arc_reader_ssl_" will be added to the PHP Stream Context if the PHP engine supports "stream_socket_client".

$conf = array(
  'arc_reader_ssl_local_cert' => '/some/path/to/somecertAndKey.pem',
  'arc_reader_ssl_passphrase' => 'somePass',
  'arc_reader_ssl_allow_self_signed' => true,
);
$reader = ARC2::getComponent('Reader', $conf);