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.

36 ” cURL Extension<br />

Simple R equest and R esponse H andling<br />

<br />

Let’s look at this line by line.<br />

• curl_init is called and passed ’http://localhost.example/path/<strong>to</strong>/form’ as<br />

the URL for the request. N ote this parameter is optional and can also be specified<br />

by calling curl_se<strong>to</strong>pt <strong>with</strong> the cURL session handle ($ch in this case), the<br />

CURLOPT_URL constant, and the URL string.<br />

• curl_se<strong>to</strong>pt is called <strong>to</strong> set the configuration setting represented by the<br />

CURLOPT_RETURNTRANSFER parameter <strong>to</strong> have a value of true. This setting will<br />

cause curl_exec <strong>to</strong> return the HTTP response body in a string rather than outputting<br />

it directly, the latter being the default behavior.<br />

• curl_exec is called <strong>to</strong> have it execute the request and return the response body.<br />

• curl_close is called <strong>to</strong> explicitly close the cURL session handle, which will no<br />

longer be reusable after that point.<br />

A useful setting worth mentioning early on is CURLOPT_VERBOSE, which outputs debugging<br />

information when set <strong>to</strong> true. This output is sent <strong>to</strong> either stderr (the default)<br />

or the file referenced by the value of the CURLOPT_STDERR.<br />

Contrasting GET and POST<br />

Ob viously the cURL extension has other functions, but by and large most HTTP requests<br />

made using the cURL extension will follo w the sequence of operations sho wn<br />

in the abo ve example. Let’s compare this <strong>with</strong> a POST request.<br />

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

Saved successfully!

Ooh no, something went wrong!