Jump to content

How to keep CURL session alive?


beyzad

Recommended Posts

This won't work? I believe I've done this before...

<?php
//Create the curl session
$curl_handle=curl_init();

//Go to site 1
curl_setopt($curl_handle,CURLOPT_URL,'http://example.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
$site1 = curl_exec($curl_handle);

//Now go to site 2
curl_setopt($curl_handle,CURLOPT_URL,'http://www.asdf.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
$site2 = curl_exec($curl_handle);

//Now close
curl_close($curl_handle);

I assume you're logging into a website first and then trying to retrieve some information? You'll need to specify a COOKIEJAR so that any cookies created by the first request (i.e. the session id) can be stored and used in the second request.

 

And no, you don't make two calls to curl_init().

I fount this on php.net:

 

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_URL, 'http://site.com/page1.php');
$result1 = curl_exec($ch);

curl_setopt($ch, CURLOPT_URL, 'http://site.com/page2.php');
$result2 = curl_exec($ch);

curl_close($ch);
?>

 

But i still cant understand how to grab somethings from the first URL, then post them to the second url?

 

Please help me.

To get something from the first page to use in the second request, you'll need to extract the information from the first request. You have the source of the page stored in $result1, so you could use regular expressions to get the data you need (e.g. preg_match).

 

Without actually knowing what sort of information you're trying to retrieve, it's hard to give you any more specific help than that.

I'm not overly convinced on why you'd want to do this...but anyway.

 

You'll need to make the request to the first page, storing the cookie that is set and extracting the image name. Of course, if the image name is always the same, you don't actually need to bother, though you do still need to make the request to the first page to have the cookie set. Show the user the image. Then make the second request, providing the cookie data you saved from the first one.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.