这是使用 PHP CURL 发出 API 请求的最佳方式吗?

发布于 2024-07-23 18:33:19 字数 829 浏览 5 评论 0原文

我有一个网站,有一个简单的 API,可以通过 http 使用。 我希望利用API并一次提交大约1000-1500次数据。 这是他们的 API: http://api.jum.name/

我已经构建了 URL 来制作但现在我想知道发出这些 1000-1500 个 API GET 请求的最佳方法是什么? 这是我正在考虑的 PHP CURL 实现:

$add = 'http://www.mysite.com/3rdparty/API/api.php?fn=post&username=test&password=tester&url=http://google.com&category=21&title=story a&content=content text&tags=Season,news';
curl_setopt ($ch, CURLOPT_URL, "$add");
curl_setopt ($ch, CURLOPT_POST, 0);
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'files/cookie.txt');
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$postdata = curl_exec ($ch);

我应该在每次提交时关闭 CURL 连接吗? 我可以用更好的方式重写上面的内容,使这 1000-1500 次提交更快吗?

谢谢大家

I have a site that has a simple API which can be used via http. I wish to make use of the API and submit data about 1000-1500 times at one time. Here is their API: http://api.jum.name/

I have constructed the URL to make a submission but now I am wondering what is the best way to make these 1000-1500 API GET requests? Here is the PHP CURL implementation I was thinking of:

$add = 'http://www.mysite.com/3rdparty/API/api.php?fn=post&username=test&password=tester&url=http://google.com&category=21&title=story a&content=content text&tags=Season,news';
curl_setopt ($ch, CURLOPT_URL, "$add");
curl_setopt ($ch, CURLOPT_POST, 0);
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'files/cookie.txt');
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$postdata = curl_exec ($ch);

Shall I close the CURL connection every time I make a submission? Can I re-write the above in a better way that will make these 1000-1500 submissions quicker?

Thanks all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2024-07-30 18:33:19

如果您可以访问 php 5.2+,我强烈推荐 php 的curl_multi。

这允许您并行处理多个curl请求,在这种情况下这肯定会派上用场。

相关文档: https://www.php.net/manual/en/ref .curl.php
用法示例:http://www.somacon.com/p537.php

If you have access to php 5.2+ I would highly recommend php's curl_multi.

This allows you to process several curl requests in parallel, which in this case would definitely come in handy.

Related documentation : https://www.php.net/manual/en/ref.curl.php
An example usage : http://www.somacon.com/p537.php

┼── 2024-07-30 18:33:19

默认情况下,PHP 的curl 会重用一个连接来多次调用curl_exec()。

因此,在这种情况下,您只需使用由curl_init 获得的curl 句柄,如果对curl_exec() 的调用之间的URL 匹配,它将发送一个“Connection: keep-alive”标头并重用该连接。

不要关闭连接,也不要设置 CURLOPT_FORBID_REUSE

另请参阅此处:

持久/使用 PHP Curl 库保持 HTTP 活动?

PHP's curl, by default, reuses a connection for multiple calls to curl_exec().

So in this case, you just ruse the curl handle, you got by curl_init and if the URL matches between calls to curl_exec(), it will send a "Connection: keep-alive" header and reuse the connection.

Do not close the connection and do not set CURLOPT_FORBID_REUSE

also see here:

Persistent/keepalive HTTP with the PHP Curl library?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文