在PHP中同时调用多个API [英] Multiple API calls simultaneously in PHP

查看:372
本文介绍了在PHP中同时调用多个API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于一个在搜索页上工作的场景. /p>

此搜索页必须同时对以下5个搜索引擎进行API调用:

  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/

想法是从所有5个API调用中收集数据,并将其存储并呈现给用户以获取匹配的数据.

让我以仅一个搜索引擎API调用为例,以"KIJIJI"为例.

示例网址: 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}

令牌{CITY},{MAKE}等将替换为用户输入的搜索值.

搜索具有实值的网址:

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

基本上,搜索URL进入页面提取器URL内,以获取所有找到的结果的列表. api调用将大量网址返回到页面.

个人结果提取器:

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

第二个api调用返回每个URL的信息.

您可以通过处理示例,页面提取器和单个结果提取器并将其粘贴到浏览器中来进行实时测试. do会返回json结果.

现在,我必须为单个搜索查询调用以上所有5个搜索引擎.我有每个搜索引擎的api网址.

也请看下面的图片.

如何通过为单个搜索查询调用多个api调用来实现此目标?它涉及多线程吗?

解决方案

要在PHP中同时发布内容,您需要某种非阻塞I/O-类似于node.js或javascript中的ajax.您可以依次执行所有操作(一个接一个),但是这会浪费时间,具体取决于所有服务的响应时间(5 x响应时间+最终处理).您可以尝试 ReactPHP 或直接使用PHP扩展,例如 libevent 用于非阻塞I/O.从PHP 5.5开始,您可以使用 yield进行异步模拟.

它不是完全多线程的,而是异步处理的.我个人从未需要在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.

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

解决方案

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.

这篇关于在PHP中同时调用多个API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆