Welcome Guest, Not a member yet? Register   Sign In
cURL sessions
#1

[eluser]rossmurphy[/eluser]
How long can/should i keep a cURL session open? I dont want to keep opening and closing on every API call. So should i just keep it open while the user is on the site? will it time out by itself?
#2

[eluser]Colin Williams[/eluser]
You can cache the results of the API calls, but you can't keep a line open "while the user is on the site." Remember that browser sessions are essentially faked by the browser keeping information about the user in cookies.
#3

[eluser]rossmurphy[/eluser]
I can't cache most of the results. The site is a gaming site based on api calls, where information changes all the time. Results are cached for a a limited amount of time for some calls but others need up to date info. So i was curious to how long i could keep the cURL init open. At the moment i am initiating the cURL when the controller is loaded, then 2 or 3 api calls are made and then i close the cURL init.
#4

[eluser]bretticus[/eluser]
That depends on a few things that are probably out of your control. Foremost...

That cURL session is probably no different than a session for your browser. And that means a cookie is stored in your browser cache that will be submitted with all it's contents over and over again. Because that's how cookies work.

In other words, when a site gives you a cookie, you always give it back from that point on. In the case of sessions, you have a GUID that identifies you to the remote server each time you connect. Sometimes--as in the case of typical CI sessions--all the session information is inside the cookie. Most of the time--the traditional sense of a session--you have the former. That is, the cookie only contains a GUID and the server caches your session information remotely. In this more common scenario, the server administrator (or the developers) decide how long your session is valid between page requests. That is, how long should the server wait until your next page request before dumping your session data to garbage collection.

Assuming that this API uses a session cookie for subsequent requests (many just require a SOAP authentication header or a token sent in the URL that identifies you each time) you will not maintain a session in this sense unless you keep a cookie file. Remember that HTTP is stateless. Like Collin said, your browser "fakes" a session. There is no connection that stays open. You send a request, you get a response. done. By sending that cookie back with the GUID each time, you emulate a session between you and the server.
#5

[eluser]rossmurphy[/eluser]
So by closing the cURL init i am basically terminating the cookie?
#6

[eluser]bretticus[/eluser]
By closing the cURL init, you are closing your connections to whatever endpoints that you are using and releasing any associated resources in memory. cURL may have performed several posts/gets in the time that your script ran (depending on your programming) but only one client using your curl scripts at a time is possible.

There *may* be a way to gain one less transaction per user by using a cookie file. This completely depends on the API you are using of course. See the cookie options in the php manual.




Theme © iAndrew 2016 - Forum software by © MyBB