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

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

问题描述

我正在处理搜索页面.>

此搜索页面必须同时为以下 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__/c174l80002a138a554a1__600000000

页面提取器 URL:

https://api.import.io/store/data/7ddb89d9-1dba-464f-a8e1-d522b50e8c4c/_query?input/webpage/url=http://www.kijiji.ca/b-汽车-camions/盛大蒙特利尔/他者+型+的+ carrosserie__berline__bicorps__cabriolet__coupe__familiale  - 本田雅阁2010 __/c174l80002a138a54a1000054a68价格= __ 5000和放大器;公里= __ 13000&放大器; _user = 8df097bf-2f5d-4509-b13e-299d05bad826和放大器; _apikey = H76otVMlVTG2KIW9fCZjWPtf4KzWFmlNBzbD2WIy9qOKSwIPgGxmUFVmTV9dDrORwcTtMBS1zZVLXSdEd9yfPQ%3D%3D

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

个人结果提取器:

https://api.import.io/store/data/e51c6ba5-fbf0-4614-b6a1-4d522b6b6ecf/_query?input/webpage/url=http://www.kijiji.ca/v-汽车-camions/伐-里沃 - 北/2013-本田雅阁运动型轿车/1062203732 siteLocale = en_CA&安培; _user = 8df097bf-2f5d-4509-b13e-299d05bad826&安培; _apikey = H76otVMlVTG2KIW9fCZjWPtf4KzWFmlNBzbD2WIy9qOKSwIPgGxmUFVmTV9dDrORwcTtMBS1zZVLXSdEd9yfPQ%3D%3D

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

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

现在的问题是我必须为单个搜索查询调用上述所有 5 个搜索引擎.我有每个搜索引擎的 api url.

也请看下图.

如何通过为单个搜索查询调用多个 api 调用来实现这一点?它是否涉及多线程?

解决方案

请阅读这篇文章

它解释了我们如何使用 curl 方法

curl_multi_exec

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?

解决方案

Please read this article

It explains how we can use the curl method

curl_multi_exec

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

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