2012-03-03 2 views
3

curl을 사용하여 두 가지 작업을 수행하려고합니다. 1. 관리 페이지에 로그인 2. 양식 제출 (사용자 추가) 첫 번째 메시지는 잘 표시되지만 두 번째 메시지는 표시되지 않습니다. .여러 동작으로 말림

$ch1 = curl_init(); 
$ch2 = curl_init(); 

curl_setopt($ch1, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1"); 
curl_setopt($ch1, CURLOPT_COOKIEJAR, "cookie.txt"); 
curl_setopt($ch1, CURLOPT_COOKIEFILE, "cookie.txt"); 
curl_setopt($ch1, CURLOPT_URL, "http://admin.example.com/admin"); 
curl_setopt($ch1, CURLOPT_POST, 1); 
curl_setopt($ch1, CURLOPT_POSTFIELDS, "user=admin&pass=password"); 
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1); // allow redirects 
curl_setopt($ch1, CURLOPT_RETURNTRANSFER,1); // return into a variable 


curl_setopt($ch2, CURLOPT_URL, "http://admin.example.com/admin/adduser"); 
curl_setopt($ch2, CURLOPT_POST, 1); 
curl_setopt($ch2, CURLOPT_POSTFIELDS, "newu=demo&pass=password"); 
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, 1); 

$mh = curl_multi_init(); 
    curl_multi_add_handle($mh, $ch1); 
    curl_multi_add_handle($mh, $ch2); 

    // execute all queries simultaneously, and continue when all are complete 
    $running = null; 
    do { 
     curl_multi_exec($mh, $running); 
    } while ($running); 

//close the handles 
curl_multi_remove_handle($mh, $ch1); 
curl_multi_remove_handle($mh, $ch2); 
curl_multi_close($mh); 

답변

4

동일한 cURL 핸들을 사용하여이 두 요청을 모두 수행 할 수 있습니다. 이 경우 curl_multi_exec을 사용할 때의 문제는 각 컬 핸들마다 다른 옵션이 있으며 $ch2은 쿠키를 참조하지 않는다는 것입니다.

또한 curl_multi_exec은 요청을 병렬로 처리하므로 로그인 요청이 완료되거나 시작되기 전에 사용자를 추가하려고 시도 할 수 있습니다.

이 대신에 $ch을 사용하여 로그인 한 다음 다시 사용하여 사용자를 추가하는 방법을 보여줍니다. 서버가 keep-alive을 지원하는 경우 keep-alive 헤더를 추가 할 수 있으며 동일한 소켓 연결이 두 번째 요청에 다시 사용됩니다. 여기

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1"); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt"); 
curl_setopt($ch, CURLOPT_URL, "http://admin.example.com/admin"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "user=admin&pass=password"); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable 

$res = curl_exec($ch); 

// check $res here to see if login was successful 

curl_setopt($ch, CURLOPT_URL, "http://admin.example.com/admin/adduser"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "newu=demo&pass=password"); 

$res = curl_exec($ch); 

// check $res to see that the user was successfully created 

curl_close($ch); 

는 로그인 후 컬을 사용하여 동일한 사이트에 여러 개의 시리얼 요청을하는 방법을 보여주는 몇 가지 다른 답변입니다.
Login to Google with PHP and Curl, Cookie turned off?
Retrieve Android Market mylibrary with curl
PHP Curl - Cookies problem

0

문제는 curl_multi를 사용하여, 당신은 동시에 두 요청을 실행하는 것입니다 : 다음은 내 코드입니다. 요청서를 제출하는 양식은 로그인 요청과 동시에 전송되므로 로그인 쿠키는 아직 사용할 수 없습니다.

또한 쿠키를 양식 요청에 전혀 전달하지 않습니다. 이것을 잊어 버렸습니다.

양식 요청에 적절한 로그인 쿠키를 사용할 수 있도록 두 가지 요청을 별도로 수행해야합니다.