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.

52 ” pecl_http PECL Extension<br />

• After HttpRequest is instantiated, its request method is explicitly set using the<br />

constant HTTP_METH_POST.<br />

• Request options are passed in this time, specifically the value for the Referer<br />

header. They are specified as the fourth parameter of http_post_fields and<br />

via setOptions.<br />

• As <strong>with</strong> the GET request, the return values of http_post_fields and send are of<br />

the same types (string and HttpMessage) as their respective counterparts in the<br />

earlier GET example.<br />

• Because http_post_fields does not have an object <strong>to</strong> use in retaining state, it<br />

instead allo ws the option <strong>to</strong> pass in a fifth parameter $info in which it will s<strong>to</strong>re<br />

information about the request and response. See http://php.net/http_get for<br />

an example of the information s<strong>to</strong>red in $info.<br />

i<br />

Alternative M ethods and M ore Options<br />

F or request method constants beyond GET and POST used by the procedural API, see<br />

http://php.net/manual/en/http.constants.php. F or more on available request options<br />

for both APIs, see http://php.net/manual/en/http.request.options.php.<br />

H andling H eaders<br />

When using the procedural API, limited request header information is available<br />

via the $info parameter of functions <strong>to</strong> issue requests. Response headers are included<br />

in the string returned by those function calls. When the string is passed <strong>to</strong><br />

http_parse_message, it returns an object <strong>with</strong> a headers property, which is an associative<br />

array of header values indexed by header name.<br />

Cus<strong>to</strong>m request headers can be set via the ’headers’ request option, which is formatted<br />

as an associative array <strong>with</strong> header names for keys pointing <strong>to</strong> corresponding<br />

header values (as opposed <strong>to</strong> cURL, which uses an enumerated array <strong>with</strong> one<br />

header name-value pair per array item). Belo w is an example of cus<strong>to</strong>m headers in a<br />

request options array.

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

Saved successfully!

Ooh no, something went wrong!