We already looked at how output buffering, and more specifically the ob_get_contents() function, allows you to catch output before it gets to your visitor and manipulate it as you want. As it is, calling curl_exec() retrieves and outputs the page, but it would be nice to have the retrieved content stored in a variable somewhere for use when we please. Now, to improve on the previous script, it would be good if we actually had some control over the output of our retrieved HTML page. The final function, curl_close(), takes a Curl resource as its only parameter, closes the Curl session, then frees up the associated memory. ![]() The return value of curl_exec() is true/false by default, although we will be changing that soon. ![]() In the example we use CURLOPT_URL, which is used to set the URL for Curl to work with, and so the working URL is set to the third parameter - elementary, really.Ĭalling curl_exec() means, "We're finished setting our options, go ahead and do it", and you need to pass precisely one parameter: the Curl resource to use. There are a huge number of constants you can use for settings, and many of these are listed shortly. In the script above, we use curl_setopt() to do that for clarity, but it is all the same.Ĭurl_setopt() takes three parameters, which are the Curl instance to use, a constant value for the setting you want to change, and the value you want to use for that setting. It has just one optional parameter: if you pass a string into curl_init(), it will automatically use that string as the URL to work with. The curl_init() function returns a Curl instance for us to use in later functions, and you should always store it for later. Curl's functionality is, for the most part, largely manipulated through repeated calls to curl_setopt(), and it is this that distinguishes how Curl operates. As mentioned already, of these four only the second is complicated - the rest stay as you see them. ![]() There are four functions in that simple script, which are curl_init(), for initialising the Curl library, curl_setopt(), for setting Curl options, curl_exec(), for executing the Curl query, and curl_close(), for shutting down the Curl system. Thanks to PHP being such a straightforward language, there is actually a one-to-one mapping of steps to lines of code - that is, step 1, "Initialise Curl", is done by line one, $curl = curl_init(), etc. So, keeping the four-step Curl process in mind, this equates to: The first Curl script we are going to look at is the simplest Curl script that is actually useful - it will load a web page, retrieve the contents, then print it out. Bool curl_setopt ( resource curl_handle, string option, mixed value)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |