2010-03-29 4 views
3

나는 cUrl (PHP)을 사용하여 로그인 요청을 게시하고 응답을 쿠키 파일에 저장합니다. 두 번째 요청에서 헤더에 쿠키를 전달하고 확인을 위해 데이터를 게시합니다.curl 쿠키가 성공시 생성되지 않음

첫 번째 요청에서 쿠키 파일이 생성되지 않아 두 번째 요청이 실패하는 문제가 있습니다. 내가 잘못하고있는 곳에 나에게 제안 해주세요.

$cookiefile="/var/www/html/dimdim/cook.txt"; 
$url_log="http://my.dimdim.com/api/auth/login"; 
$p_log='request={"account":"bin6k","password":"password","group":"all"}'; 
$url_ver="http://my.dimdim.com/api/auth/verify"; 
$p_ver='request={"account":"bin6k","password":"password","group":"all"}'; 

$ch = curl_init(); 
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); 

curl_setopt($ch, CURLOPT_URL,$url_log); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $p_log); 

ob_start();  // prevent any output 
$retval=curl_exec ($ch); // execute the curl command 
ob_end_clean(); // stop preventing output 
curl_close ($ch); 
//print_r($retval); 
unset($ch); 


$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); 
curl_setopt($ch, CURLOPT_URL,$url_ver); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $p_log); 

$buf2 = curl_exec ($ch); 

curl_close ($ch); 

echo "<PRE>".htmlentities($buf2); 

답변

0

curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);도 거기에 추가해보십시오.

또한 각 요청에 대해 새로운 컬 인스턴스를 시작할 필요가 없습니다. 여러 요청에 대해 동일한 인스턴스를 재사용 할 수 있습니다. 매번 적절한 CURLOPT (url, postfields, get 등)를 설정하면 컬이 내부적으로 항목을 정렬합니다.

0

cookijar 옵션이 추가되어이 기능을 시도해보십시오

function execute($toLoad) { 

    if (!preg_match('/^http/', $toLoad)) { 
     $toLoad = 'http://'.$toLoad; 
    } 
    $cookiefile = APP_PATH.'tmp/cookie.txt'; 
    $data = array();  
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,10); 
    curl_setopt($ch, CURLOPT_TIMEOUT,10); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
    curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.6) Gecko/20091216 Fedora/3.5.6-1.fc11 Firefox/3.5.6 GTB6"); 
    curl_setopt($ch, CURLOPT_URL, $toLoad); // The page to download 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_REFERER, 'http://somesite.com/'); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);  

    $data['data'] = curl_exec($ch); 
    $data['status'] = curl_getinfo($ch); 

    //$this->out(curl_error($ch)); 
    //$this->out(curl_getinfo($ch));  
    //$this->out(''); 
    //$this->out($data); 
    //$this->out(''); 

    curl_close($ch); 

    return $data; 
} 

당신은 또한 설정 - 쿠키에 대한 반환 된 데이터의 헤더를 읽어보세요 수 :

다음 curlopt의 사용 SOMEKEY을

if ($header) { 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cookie:'.$header)); 
} 

고마워요.

2

저는이 문제를 동창에 사용했습니다. Windows 로컬 호스트 서버에있는

curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies.txt"); 
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookies.txt"); 
관련 문제