开发者

Unable to exchange Session Data in CURL

I call invoke.php using CURL from curl.php from my localhost. In invoke.php i store some data in session. But when i try to access those session data from curl.php, don't get those session data. How i get those values?

Content of curl.php

`include_once ('session.php');

$handles = array();

$urlArray = array('http://localhost/invoke.php' );

foreach($urlArray as $url){

// create a new single curl handle

$ch = curl_init();



curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_TIMEOUT, 30);



// add this handle to the multi handle

curl_multi_add_handle($mh,$ch);



// put the handles in an array to loop this later on

$handles[] = $ch;

}

// execute the multi handle

$running=null;

do {

curl_multi_exec($mh,$running);

// added a usleep for 0.25 seconds to reduce load

usleep (250000);

} while ($running > 0);

// get the content of the urls (if there is any)

for($i=0;$i

{

// get the content of the handle

// $output.= curl_multi_getcontent($handles[$i]);

// remove the handle from the multi handle

curl_mul开发者_StackOverflow中文版ti_remove_handle($mh,$handles[$i]);

}

echo SessionHandler :: getData('DATA'); `

Content of invoke.php

include_once ('session.php');

echo SessionHandler :: setData('DATA', 'HELLO WORLD');


There are two possible issues:

  1. You can't be sure that both scripts will access same session (have same session id), actually I can bet they have separate sessions. Of course, you could enforce that by sending the session id from invoke.php to curl.php by adding an extra parameter tot the URL and then use that parameter to force the session id in curl.php

  2. Second possible issue is that the session variables are read on session_start (that's when the $_SESSION is populated), but you modify the session content after you start the session (in session.php, I assume), that's why any changes (even if the conditions on 1. will be met), will not reflect into already opened session in invoke.php. I think you should force session to close and restart it.


I had the same issue. This is how I resolved it:

function getContent($url){
    $cookiesStr = '';
    foreach($_COOKIE as $k => $v){
        $cookiesStr.= $k . '=' . $v . '; ';

    }#end COOKIE
    $cookiesStr = rtrim($cookiesStr, ' ');

    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
    curl_setopt ($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_COOKIE, $cookiesStr);
    ob_start();

    curl_exec ($ch);
    curl_close ($ch);
    $string = ob_get_contents();

    ob_end_clean();

    return $string;    

}#end getContent

You have to have a tmp cookie jar or it will produce an infinite loop. You have to send cookies or you will not get a result.


I able to solve this problem. I set curl_

setopt($ch, CURLOPT_COOKIE,session_name().'='.session_id());

to use same session id and use php's session_write_close() function to write session data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜