본문 바로가기

[Program]/[PHP]

curl session 공유하기..

curl 사용시 session 이 공유되지 않는 현상이 있음.


해당 부분을 세션을 강제로 공유하게 만드는 방법.



//헤더 추출함수

function get_http_header_as_array($rawheader){

$header_array = array();

$header_rows = explode("\n",$rawheader);

for($i=0;$i<count($header_rows);$i++){

$fields = explode(":",$header_rows[$i]);

if($i != 0 && !isset($fields[1])){//carriage return bug fix.

 if(substr($fields[0], 0, 1) == "\t"){

 end($header_array);

 $header_array[key($header_array)] .= "\r\n\t".trim($fields[0]);

 }

 else{

 end($header_array);

 $header_array[key($header_array)] .= trim($fields[0]);

 }

}

else{

$field_title = trim($fields[0]);

if (!isset($header_array[$field_title])){

 $header_array[$field_title]=@trim($fields[1]);

}

else if(is_array($header_array[$field_title])){

 $header_array[$field_title] = array_merge($header_array[$fields[0]], array(trim($fields[1])));

}

else{

 $header_array[$field_title] = array_merge(array($header_array[$fields[0]]), array(trim($fields[1])));

}

}

}

 

return $header_array;

}

//헤더 추출함수



$URL = "http://www.bongfeel.com";

$Params = array("userid"=>"user","userpw"=>"1234");

curl_setopt($ch, CURLOPT_URL, $URL);

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($Params));

curl_setopt($ch, CURLOPT_HEADER, true); //헤더정보를 받아옴.

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

curl_setopt($ch, CURLOPT_TIMEOUT, 30);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


$Result = curl_exec($ch);

$v = explode("\r\n\r\n",$Result);

$Result = $v[1];

$headers = http_header_array($v2[0]); // 헤더정보 추출



$cookie = $headers['Set-Cookie']."; path=/";  //세션정보 변수저장.


$URL = "http://www.bongfeel.com";

$Params = array("KEY"=>"key");

curl_setopt($ch, CURLOPT_URL, $URL);

curl_setopt($ch, CURLOPT_COOKIE, $cookie);    //세션정보를 같이 넘김.

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($Params));

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

curl_setopt($ch, CURLOPT_TIMEOUT, 30);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);



받아온 세션정보를 그대로 넘겨주면 세션이 유지가 된다.