Keeping session alive with Curl and PHP
You also need to set the option CURLOPT_COOKIEFILE
.
The manual describes this as
The name of the file containing the cookie data. The cookie file can
be in Netscape format, or just plain HTTP-style headers dumped into a
file. If the name is an empty string, no cookies are loaded, but
cookie handling is still enabled.
Since you are using the cookie jar you end up saving the cookies when the requests finish, but since the CURLOPT_COOKIEFILE
is not given, cURL isn't sending any of the saved cookies on subsequent requests.
Persistent/keepalive HTTP with the PHP Curl library?
cURL PHP documentation (curl_setopt) says:
CURLOPT_FORBID_REUSE
-TRUE
to force
the connection to explicitly close
when it has finished processing, and
not be pooled for reuse.
So:
- Yes, actually it should re-use connections by default, as long as you re-use the cURL handle.
- by default, cURL handles persistent connections by itself; should you need some special headers, check CURLOPT_HTTPHEADER
- the server may send a keep-alive timeout (with default Apache install, it is 15 seconds or 100 requests, whichever comes first) - but cURL will just open another connection when that happens.
Php curl keep alive connection
Sending multiple messages, and using keep-alive
are two separate things. With Nexmo (as the FAQ mentions) you can only send a single SMS per HTTP request. To send multiple SMS, you just have to make multiple HTTP requests.
By default, Nexmo will allow your account to make 5 requests per second to the SMS API. If you want to maximize your throughput, you need to make sure you're making the request as fast as possible (or really, just at least as fast at that 5/second rate limit).
That's where the keep-alive
comes into play, making sure you're sending the requests as fast as possible. The curl_setop
docs reference a CURLOPT_FORBID_REUSE
which allows:
TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse.
So by default, curl is trying to use keep-alive
, assuming you reuse the curl handle. See this question for more details on that.
Borrowing this code from the quickstarts here (disclosure, I'm the author of those):
<?php
$url = 'https://rest.nexmo.com/sms/json?' . http_build_query([
'api_key' => API_KEY,
'api_secret' => API_SECRET,
'to' => YOUR_NUMBER,
'from' => NEXMO_NUMBER,
'text' => 'Hello from Nexmo'
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
If you use curl_setop()
to set a new CURLOPT_URL
with a different number / message (which reuses the curl handle) curl should be using keep-alive
by default.
But keep in mind, this doesn't change how you send multiple messages with Nexmo, it's just a way to optimize the speed at which you send the messages.
How to keep curl session alive between two php processes?
I figured out how it works)
If you want to keep session just copy set-cookie
option from header response to some file. After that you can use this cookies for next connection. You have not to do authorization again.
PHP and cURL: How to keep session alive within multiple requests?
The problem wasn't there because of a session problem. The reason why it did not work was that the url had entities in it.
html_entity_decode fixed it.
Setting --keepalive-time for curl in php
If you are using PHP 5.5 or greater (currently 5.5, 5.6, & 7) built with cURL 7.25.0 or greater, you can set these cURL options in PHP to match the --keepalive-time
parameter (PHP <= 5.4 did not have these cURL options available):
curl_setopt($session, CURLOPT_TCP_KEEPALIVE, 1);
curl_setopt($session, CURLOPT_TCP_KEEPIDLE, 30);
curl_setopt($session, CURLOPT_TCP_KEEPINTVL, 15);
The CURLOPT_TCP_KEEPIDLE
constant in libcurl corresponds to the --keepalive-time
command line option for curl
.
See the cURL docs regarding CURLOPT_TCP_KEEPALIVE, CURLOPT_TCP_KEEPIDLE, and CURLOPT_TCP_KEEPINTVL for more info.
Note: These options are not available and can't be used if you have PHP 5.4 or lower.
Related Topics
PHP Constants Containing Arrays
String Comparison Using '==' Vs. 'Strcmp()'
Full Secure Image Upload Script
PHP 5.4 Call-time pass-by-reference - Easy Fix Available
Bulk Insertion in Laravel Using Eloquent Orm
Getting Checkbox Values on Submit
Changing Upload_Max_Filesize on PHP
MySQLi::MySQLi(): (Hy000/2002): Can't Connect to Local MySQL Server Through Socket 'MySQL' (2)
Convert Command Line Curl to PHP Curl
How to Convert a Number to a Word in PHP
How to Loop Through Two Arrays At Once
Html Element Array, Name="Something[]" or Name="Something"
How to Delete a Line from the File With PHP
Get Specific Columns Using "With()" Function in Laravel Eloquent
Correctly Determine If Date String Is a Valid Date in That Format