Problem: In PERL script, we use loop to request the API in PHP from PERL repeatedly. When request the API(PHP) from PERL using GET, the session created in PHP function will be reset after next call. For example, if PERL script want to get 5 data, it will request the API 5 times in one execution.
We want to save each id from this 5 data, mean every time the API is excuted, stored the required data in SESSION. The problem is, for the first request, the data is stored in SESSION. After the next call, the data saved in SESSION will be not retrieved as it already created the new SESSION data.
Solution: Every time we call the API, we set the same cookie on the request header using LWP::UserAgent. PHP will retreive the SESSION data based on the cookie passed from PERL. This cookie contain the previously used session id.
my $cookie;
require LWP::UserAgent;
require HTTP::Cookies;
my $ua = LWP::UserAgent->new;
$cookie = HTTP::Cookies->new if !defined $cookie;
$ua->cookie_jar($cookie);
$ua->timeout(300);
my $contents = $ua->get($url); # print Dumper($cookie); # die();
if (defined($contents->{"_content"}) && $contents ne "") {
#do something }
else { #do something }