Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple API calls simultaneously in PHP

I'm in a scenario to work on a search page.

This search page has to make API calls for 5 below search engines simultaneously:

  1. http://www.kijiji.ca/
  2. http://www.lespac.com/
  3. http://www.autonet.ca/
  4. http://www.autotrader.ca/
  5. http://www.carpages.ca/

The idea is to collect data from all 5 API calls and store and render it to user for matched data.

Let me take example of only one search engine API call, let's say for 'KIJIJI'.

SAMPLE URL: http://www.kijiji.ca/b-autos-camions/grand-{CITY}/autre+type+de+carrosserie__berline__bicorps__cabriolet__coupe__familiale-{MAKE}-{MODEL}-{MIN YEAR}__/c174l80002a138a54a1000054a68?price=__{Max Price}&kilometres=__{MAX KM}

The token {CITY}, {MAKE} etc will be replaced with user entered search value.

SEARCH URL WITH REAL VALUES:

http://www.kijiji.ca/b-autos-camions/grand-montreal/autre+type+de+carrosserie__berline__bicorps__cabriolet__coupe__familiale-Honda-Accord-2010__/c174l80002a138a54a1000054a68?price=__5000&kilometres=__13000

PAGE EXTRACTOR URL:

https://api.import.io/store/data/7ddb89d9-1dba-464f-a8e1-d522b50e8c4c/_query?input/webpage/url=http://www.kijiji.ca/b-autos-camions/grand-montreal/autre+type+de+carrosserie__berline__bicorps__cabriolet__coupe__familiale-Honda-Accord-2010__/c174l80002a138a54a1000054a68?price=__5000&kilometres=__13000&_user=8df097bf-2f5d-4509-b13e-299d05bad826&_apikey=H76otVMlVTG2KIW9fCZjWPtf4KzWFmlNBzbD2WIy9qOKSwIPgGxmUFVmTV9dDrORwcTtMBS1zZVLXSdEd9yfPQ%3D%3D

Basically the search URL goes inside page extractor URL to get list of all the found results. The api call return bulk of urls to the pages.

INDIVIDUAL RESULT EXTRACTOR:

https://api.import.io/store/data/e51c6ba5-fbf0-4614-b6a1-4d522b6b6ecf/_query?input/webpage/url=http://www.kijiji.ca/v-autos-camions/laval-rive-nord/2013-honda-accord-sport-sedan/1062203732?siteLocale=en_CA&_user=8df097bf-2f5d-4509-b13e-299d05bad826&_apikey=H76otVMlVTG2KIW9fCZjWPtf4KzWFmlNBzbD2WIy9qOKSwIPgGxmUFVmTV9dDrORwcTtMBS1zZVLXSdEd9yfPQ%3D%3D

This second api call return information for each individual URL.

You can test live by coping and pasting the sample, page extractor and individual result extractor into your browser. The do return json results.

Now the thing is I have to call all the above 5 search engine for single search query. I've api url for each search engine.

Please also have a look at below image.

enter image description here

How can I achieved this by calling multiple api calls for single search query? Does it involve multi threading?

like image 684
Irfan Avatar asked Apr 09 '15 07:04

Irfan


2 Answers

Please read this article

It explains how we can use the curl method

curl_multi_exec
like image 200
Muhammed Shihabudeen Labba A Avatar answered Sep 28 '22 07:09

Muhammed Shihabudeen Labba A


For issueing something simultaneously in PHP, you need some kind of non-blocking I/O - similary like in node.js or ajax in javascript. You can do all actions in sequence (one by one) but it will be time consumpting depending on response time of all services (5 x reponse time + final processing). You can try ReactPHP or directly PHP extension like libev, libevent for non-blocking I/O. From PHP 5.5 you can use yield for async simulation.

It is not exactly multithreading but asynchronous processing. I have personally never had a need to use any of these techniques in PHP, so I can not offer code example or additional experiences.

like image 37
kba Avatar answered Sep 28 '22 09:09

kba