I'm new to curl library. Google solves most of my questions such as how to execute a curl. However, there is one thing still confuses me, that is when we set
curl_setopt($ch,CURLOPT_URL,'http://example.com')
will curl download the content from the targeted website? if not, then how curl posts the content? Thanks!
A typical session of curl
in PHP looks like this:
// Initialize curl
$ch = curl_init('http://www.google.com');
// Configure curl as needed, depending on your application
curl_setopt_array($ch, array(
'CURLOPT_FOLLOWLOCATION' => TRUE,
'CURLOPT_RETURNTRANSFER' => TRUE,
'CURLOPT_CONNECTTIMEOUT' => 3,
// ... other options here ...
));
// Do the request
$page = curl_exec($ch);
// Cleanup
curl_close($ch);
Sometimes you may want to do several requests using the same options. For subsequent requests you can repeat the code above with a different URL (or encapsulate it in a function for convenience).
Or you can reuse the existing resource and issue multiple requests before closing it.
To do this call curl_setopt($ch, CURLOPT_URL, 'http://example.com')
to change the URL then call curl_exec()
again to get the new page.
Repeat these two steps as many times as you need before calling curl_close()
.
To answer your question, curl_setopt()
and curl_setopt_array()
only prepare the curl
object and the HTTP request. curl_exec()
is the function that actually sends the request and returns (or displays) the content it receives.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.