Search code examples
phpmultithreadingapiwrapperovh

Multiple API requests with OVH API wrapper


I am trying to create a web interface to interact with OVH's API using the official PHP wrapper.

First I get my IP list :

$ips = $ovh->get('/ip');

But then, for each IP address I want to get the related informations and put those in a table, so I do :

foreach ($ips as $ip) {
    $ip_infos = $ovh->get('/ip/' . $ip_api);
}

It works, but for each call, I have to wait for the previous one to come back. So it takes like 30~40sec to load the full page because of the ~100 IP.

Is there a way to send only one big request instead of many little ones with the OVH PHP wrapper ? Or maybe do multithreading ? Anything that could speed up significantly the requests.


Solution

  • Unless your PHP engine is build with "ZTS" (Zend Thread Safety) and you have the pthread extension installed, this can not be achieved using the only the official PHP wrapper. If using plain PHP is mandatory, you may patch the wrapper to use Curl Multi. See http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/ for an example.

    Another way to do it is to use an hybrid approach using client side JS.

    1. The PHP returns a page with a list of IPs
    2. The JS requests the IPs details asynchronously

    This will effectively trigger multiple parallel requests and improve user experience. Actually, this is what OVH customer interface does under the hood.