25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

248 ” Streams and Network Programming<br />

include(); for example, the following is perfectly valid (depending on your configuration):<br />

include ’http://phparch.com’;<br />

This capability is, of course, something that you should both love and fear: on one<br />

hand, it allows you include remote files from a different server. On the other, it represents<br />

a potential security hole of monumental proportions if the wrong person gets<br />

their hands on your code.<br />

Stream Contexts<br />

Stream contexts allow you to pass options to the stream handlers that you transparently<br />

use to access network resources, thus allowing you to tweak a handler’s behaviour<br />

in ways that go beyond what normal file functionality can do. For example,<br />

you can instruct the HTTP stream handler to perform a POST operation—which is<br />

very handy when you want to work with Web services.<br />

Stream contexts are created using stream_context_create():<br />

$http_options = stream_context_create(array(<br />

’http’ => array(<br />

’user_agent’ => "Davey Shafiks Browser",<br />

’max_redirects’ => 3<br />

)<br />

));<br />

$file = file_get_contents("http://localhost/", false, $http_options);<br />

In this example, we set context options for the http stream, providing our own custom<br />

user agent string (which is always the polite thing to do to help people identify<br />

the activity you perform on their server), and set the maximum number of transparent<br />

redirections to three. Finally, as you can see, we pass the newly-created context<br />

as a parameter to file_get_contents().<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

Advanced Stream Functionality<br />

While the built-in stream handlers cover the most common network and file operations,<br />

there are some instances—such as when dealing with custom proto-

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

Saved successfully!

Ooh no, something went wrong!