PHP API 访问多个调用 [英] PHP API acess multiple calls

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

问题描述

我正在通过 PHP cURL 调用 PayPal 的 TransactionSearch API.

I am running calls onto PayPal's TransactionSearch API through PHP cURL.

遗憾的是,API 的响应速度非常慢,对于单个客户,有时需要 30 秒到 5 分钟以上(取决于从 API 返回的记录数量).

Unfortunately, the API is very slow to respond, sometimes taking anywhere from 30 seconds to more than 5 minutes (depending on the number of records returned from the API) for a single customer.

目前,脚本正在运行一个 cron 作业,并逐个循环遍历每个客户.但是,如果客户数量增加,整个过程将需要很长时间(几个小时)才能完成.这还不够好.

At the moment, the script is running off a cron job, and looping through each customer one by one. However, if the number of customers scale up, the entire process would take a very long time (few hours) to complete. This is not good enough.

本质上,我需要同时运行(和处理)多个 API 调用.实现这一目标的最佳方法是什么?

Essentially, I need to run (and process) multiple API calls simultaneously. What's the best way to achieve this?

推荐答案

由于瓶颈是远程服务器,我建议使用 curl_multi_exec.您将一次处理大量 HTTP 连接,然后在一个线程中处理它们的结果.

Since the bottleneck is the remote server, I suggest using curl_multi_exec. You'll be processing a big number of HTTP connections at once and then process their results in one thread.

这不是最快的解决方案,即在响应在多个线程中可用时立即处理,但这种方法可以使处理速度提高 50 倍以上,而无需进行重大更改.

This is not the fastest solution, which would be to process responses as soon as they're available in multiple threads, but this approach can make processing 50+ times faster without significant changes.

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

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