03.02.2014 Views

php|architect's Guide to Web Scraping with PHP - Wind Business ...

php|architect's Guide to Web Scraping with PHP - Wind Business ...

php|architect's Guide to Web Scraping with PHP - Wind Business ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

32 ” HTTP Streams W rapper<br />

$context = stream_context_create(<br />

array(<br />

’http’ => array(<br />

’ignore_errors’ => true<br />

)<br />

)<br />

);<br />

$stream = fopen(’http://localhost.example/404’, ’r’, false, $context);<br />

// $stream will be a stream resource at this point regardless of<br />

// the outcome of the operation<br />

$body = stream_get_contents($stream);<br />

$meta = stream_get_meta_data($stream);<br />

// $meta[’wrapper_data’][0] will equal something like HTTP/1.0 404<br />

// Not Found at this point, <strong>with</strong> subsequent array elements being<br />

// other headers<br />

$response = explode(’ ’, $meta[’wrapper_data’][0], 3);<br />

list($version, $status, $description) = $response;<br />

switch (substr($status, 0, 1)) {<br />

case ’4’:<br />

case ’5’:<br />

$result = false;<br />

default:<br />

$result = true;<br />

}<br />

?><br />

HTTP A uthentication<br />

The HTTP stream wrapper has no context options for HTTP authentication credentials.<br />

H o wever, it is possible <strong>to</strong> include the credentials as part of the URL being accessed.<br />

See the example belo w . N ote that credentials are not pre-encoded; this is<br />

handled transparently when the request is made. Also , note that this feature only<br />

works when Basic HTTP authentication is used; Digest authentication must be handled<br />

manually.<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!