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.

cURL Extension ” 37<br />

$data = array(<br />

’param1’ => ’value1’,<br />

’param2’ => ’value2’,<br />

’file1’ => ’@/path/<strong>to</strong>/file’,<br />

’file2’ => ’@/path/<strong>to</strong>/other/file’<br />

);<br />

$ch = curl_init();<br />

curl_se<strong>to</strong>pt($ch, CURLOPT_URL, ’http://localhost.example/process’);<br />

curl_se<strong>to</strong>pt($ch, CURLOPT_POST, true);<br />

curl_se<strong>to</strong>pt($ch, CURLOPT_POSTFIELDS, $data);<br />

curl_se<strong>to</strong>pt($ch, CURLOPT_RETURNTRANSFER, true);<br />

$response = curl_exec($ch);<br />

curl_close($ch);<br />

?><br />

H ere are the differences between this example and the previous one.<br />

• The URL is passed using the curl_se<strong>to</strong>pt function this time, just <strong>to</strong> sho w ho w<br />

<strong>to</strong> do it <strong>with</strong>out passing it <strong>to</strong> the curl_init function. This is important when<br />

reusing cURL session handles <strong>with</strong> different URLs.<br />

• CURLOPT_POST is set <strong>to</strong> true <strong>to</strong> change the request method <strong>to</strong> POST.<br />

• CURLOPT_POSTFIELDS is an associative array or preformatted query string <strong>to</strong> be<br />

used as the data for the request body. Files can be uploaded by specifying a<br />

value where the first character is @ and the remainder of the value is a filesystem<br />

path <strong>to</strong> the file intended for upload.<br />

H ere are a few other cURL configuration setting constants related <strong>to</strong> the request<br />

method.<br />

• CURLOPT_HTTPGET: Boolean that, when set <strong>to</strong> true, explicitly resets the request<br />

method <strong>to</strong> GET if it’s been changed from the default.<br />

• CURLOPT_NOBODY: Boolean that, when set <strong>to</strong> true, excludes the body from the<br />

response by changing the request method <strong>to</strong> HEAD .

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

Saved successfully!

Ooh no, something went wrong!