简体   繁体   English

在PHP中卷曲连接重用

[英]curl connection reuse in php

I use curl in my php application. 我在我的php应用程序中使用curl。 It looks like that (simplified): 看起来像这样(简化):

$handle = curl_init();
curl_exec($handle);
curl_close($handle);

As written in Persistence Is The Way to Happiness chapter, 如《 持久性是通往幸福的方式》一章所述,

A subsequent request using the same easy handle to the same host might just be able to use the already open connection! 对相同主机使用相同易用句柄的后续请求可能仅能使用已打开的连接! This reduces network impact a lot. 这样可以大大减少网络影响。

So, is it applied to this code? 那么,它适用于此代码吗? Will the connections be saved and curl_init() use the existing connection? 是否将保存连接并且curl_init()使用现有连接? If yes -- how long are they stored? 如果是,它们将存储多长时间?

I haven't tested this myself but here's how I think it should work : 我自己还没有测试过,但是我认为这应该起作用:

You create a curl instance : 您创建一个curl实例:

$handle = curl_init();

Then you set up your options, like the URL, the method (post or get) and the query string : 然后设置选项,例如URL,方法(发布或获取)和查询字符串:

curl_setopt($handle, CURLOPT_URL, "http://stackoverflow.com");

Execute the request : 执行请求:

curl_exec($handle); // execute the request

Change your options, for example change the URL : 更改选项,例如更改URL:

curl_setopt($handle, CURLOPT_URL, "http://stackoverflow.com/test/");

Execute the request again, it should be able to reuse the already open connection : 再次执行请求,它应该能够重用已经打开的连接:

curl_exec($handle);

You can do that as many times as you want with the same curl instance, and it'll reuse connections if possible. 您可以对同一个curl实例执行任意多次操作,并且如果可能,它将重用连接。

Finally close the connection and delete the curl instance when you're done : 最后,关闭连接并在完成后删除curl实例:

curl_close($handle);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM