Author: riccardo castagna
Last month viewers: 59
Package: Fast PHP CURL Multiple Requests
Read this article to learn how to use the PHP Curl extension and take advantage of the Curl Multi functions using the Fast PHP CURL Multiple Requests package.
An Introductory Anecdote to Break the Ice
Let me talk first a bit about my experience to put in context the solution that I found for the problems presented in this article. If you are in a rush, feel free to jump below to the section on The Optimization of PHP Curl Multi.
A teacher of mine during my master degree in business administration once told me that there are two kinds of people: those who are able to do things and those who are not able to do them. Those who know how to do it they work, instead of those who do not know how to do it they teach.
However, the teacher who told me this was both a great consultant on the human resources field and also a very good teacher. I personally think that the real true is that who is able to do often is also a great teacher.
I started this article with this anecdote because I feel I still have much to learn in a field that is always evolving.
I may not be the best person to teach or to write an article, but I will try to write something about I managed to solve a problem of optimization related to the PHP cURL Multi functions and about the method I used to solve this issue.
Something about My Background on Problem Solving
Allow me to make a brief break on to talk about my background related to the training experience either for you to know me better and to introduce the next topic of problem solving method.
The master degree that I took was a unique and very important two year period of my life working full time on theoretical and practical training experience.
I have had Italian and foreign professors of the highest level: teachers of important foreign universities, consultants and managers of large companies, of multinationals and even of heads of government.
The methodology was to put business cases in front of the students. Those business cases were of various kinds and natures: marketing, financial, production, psychological cases, cases related to negotiation of everything, to solve problems without teaching the skills, then after the solutions in the classroom were exposed and discussed, the teachers did the lessons with the theory and with the correct solutions of the cases.
This methodology is used to train the mind of the students to solve problems that were never seen, so you get used to the unpredictable.
To give an example related to programming let me comment on a one case that a good programmer must be able to solve. When two different people that are bookin a room of a hotel online through a booking system at the same instant and there is only one room left free. Whom to give priority and how?
In programming, to solve the problems you have to invent solutions by imagining them. There is a funny phrase I read on social media: The programmer is someone who solves problems you did not think you had and did not think they could exist.
Let me share the thought of the philosopher Karl Popper that says: life consists of problems to be solved and therefore learning to solve problems means learning to live.
Introduction to Problem Solving
Problem solving is a set of scientific methods. In life whatever you do, whether you are a doctor or a manager or a cook or like in our case PHP programmers will always deal with problems to solve.
You may know by heart all the PHP functions, even the most advanced, but if you do not learn the methods and the approaches in order to perform effective problem solving, you can often go astray.
Personally I think that to be a good PHP programmer you must also be a good analyst.
The Phases of Problem Solving
The most recent orientation identifies the process for solving a problem as a cycle consisting of seven steps:
1. Identification of the problem
2. Definition and representation of the problem
3. The formulation of a solution strategy
4. The organization of information
5. The allocation of resources
6. Control of the solution process
7. Evaluation of the effectiveness of the solution itself
Among these phases, the initial phase is very important. It includes elements such as the perception of the problem, its acceptance, the type of assessment that is made of the problem.
The identification of a situation as problematic is a crucial moment because if the problem is represented in an inaccurate way, it is much less able to solve it.
Another important phase during the analysis of the problem is that of decomposition of the problem into simpler elements.
The step 7 is the test. If the test reports errors, the problem solving cycle must be repeated.This is a very important method to work and to solve the problems.
The PHP cURL Extension
I consider the PHP cUrl extension to be the pulsating heart of the PHP. It is a very useful extension for many reasons.
In all these cases, the cURL PHP extension is fundamental.
The Optimization of PHP Curl Multi
When I started working with the PHP Curl multi functions, after I read the documentation and started testing it, I found out that it had a stability problem. I searched the Web, analyzed and tested many solutions that were proposed.
All the solutions I found on the Web did not identify the problem correctly because they were performing a wrong analysis.
Let's take a look at the basic working example on php.net:
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the two handles
//execute the handles
} while($running > 0);
//close all the handles
They focused their efforts on a loop, believing that the optimization problem was related to the execution of this loop. The loop in question is the one with the following code:
$running = null;
The solutions I found on Web that I have seen (and analyzed many of them), tried to optimize this loop by not solving the problem because ironically this loop has no problem.
The solution that I implemented optimized everything that happened before this loop. I could verify this doing many tests and simplifying the whole procedure of the function in question by analyzing it piece by piece, breaking it down into simple steps.
Here you can take look at my solution for the php_curl_multi optimized version. Basically, I rewrote the solution using a PHP class of mine called Fast PHP CURL Multiple Requests that just sends the HTTP request to the first of a list of URLs, and only then it sends HTTP requests to all the remaining URLs.
In the end it seems a simple solution but it indeed solves the problem of avoiding to overloading the remote server.
I end this article by humorously wishing you a life full of problems to solve.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
3. Why not just set CURLMOPT_MAXCONNECTS ? - Tofser (2019-04-25 08:37)
Improved stability is probably due to curl options... - 1 reply
Read the whole comment and replies
2. Curl Multiple Requests - John Rutherford (2019-04-25 06:10)
Curl stuff... - 1 reply
Read the whole comment and replies
1. Big Thank You - Terry Woody (2019-04-25 05:29)
just saying thanks...... - 0 replies
Read the whole comment and replies