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

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

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

cURL Extension ” 45<br />

$ch1 = curl_init(’http://localhost.example/resource1’);<br />

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

/* other curl_se<strong>to</strong>pt calls */<br />

$ch2 = curl_init(’http://localhost.example/resource2’);<br />

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

/* other curl_se<strong>to</strong>pt calls */<br />

$mh = curl_multi_init();<br />

curl_multi_add_handle($mh, $ch1);<br />

curl_multi_add_handle($mh, $ch2);<br />

$running = null;<br />

do {<br />

curl_multi_exec($mh, $running);<br />

} while ($running > 0);<br />

$ch1_response = curl_multi_getcontent($ch1);<br />

$ch2_response = curl_multi_getcontent($ch2);<br />

curl_multi_remove_handle($mh, $ch1);<br />

curl_close($ch1);<br />

curl_multi_remove_handle($mh, $ch2);<br />

curl_close($ch2);<br />

curl_multi_close($mh);<br />

?><br />

• Two cURL session handles $ch1 and $ch2 are initialized and configured normally.<br />

N ote that more than two can be used in this type of operation; two<br />

merely satisfy the purpose of this example.<br />

• A cURL multi handle $mh is initialized and the two session handles are added<br />

<strong>to</strong> it.<br />

• A loop is used in conjunction <strong>with</strong> the flag $running <strong>to</strong> repeatedly check (i.e.<br />

poll) the multi handle for completion of all contained operations.<br />

• curl_multi_getcontent is used on the two session handles <strong>to</strong> get the response<br />

bodies of each.

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

Saved successfully!

Ooh no, something went wrong!