* 'CURLOPT_FOLLOWLOCATION' (follow 3XX redirects) and 'CURLOPT_RETURNTRANSFER' * $moreOptions Any other options to add to the cURL request. * $method 'GET' or 'POST' 'GET' by default. * $urlParameters The array of parameters, with 'key' => 'value' format. $this->throwExceptionIfError($this->request) So, we are going to create a class that encapsulates this logic. When we are going to use cURL in a project, is recommendable to have encapsulated all the code instead of using every time the functions we have used above, because we would be repeating much code, and the possibilities of introducing errors would increase. The rest of the script, can be the same for both. The only difference between cURL POST and GET requests is the configuration of the session. And, apart from that, we are specifying that the request will be made for POST method, with CURLOPT_POST option. Those parameters are specified in CURLOPT_POSTFIELDS option. Note that, when setting the URL, we just set the URL itself without the parameters, that’s for GET requests. POST requestsįor POST requests, we have to configure the cURL options in a slightly different way:ĬURLOPT_POSTFIELDS => stringifyParameters(), We will see that the output generated by the script, is the same of that which we would have received making a search in the above search box, in this page, entering php. We can test this script, entering, for example, localhost/path/to/curl_get.php?url=&s=php in the browser. Finally, when we have ended the cURL session handling, we must close it, curl_close() function.In this case, as we configured the CURLOPT_RETURNTRANSFER option, we can save the response value into a variable.
3 Comments
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |