2017-02-24 4 views
0

Ich weiß, dass diese Frage schon oft im Stack-Overflow gestellt wurde, aber keine der Antworten löste mein Problem.Wie man Sitzung mit curl php aufrechterhält?

ich ein PHP-Skript schreibe, die curl verwendet, um remote JSP Website durchsuchen: hier ist mein Code:

<?php 
$loginUrl = 'http://ccc.hyundai-motor.com/servlet/ccc.login.CccLoginServlet'; 

$sh = curl_share_init(); 
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SHARE, $sh); 
curl_setopt($ch, CURLOPT_URL, $loginUrl); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,'the post data including my username and password and other hidden fields'); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_exec($ch); 

$ch2 = curl_init(); 
curl_setopt($ch2, CURLOPT_SHARE, $sh); 
curl_setopt($ch2, CURLOPT_URL,'http://ccc.hyundai-motor.com/servlet/ccc/admin/user/UserInfoServlet?cmd=myUserInfo'); 
curl_setopt($ch2, CURLOPT_COOKIEFILE,'cookie.txt'); 
curl_setopt($ch2, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($ch2, CURLOPT_VERBOSE, true); 
$result = curl_exec($ch2); 
print $result; 
curl_share_close($sh); 
curl_close($ch); 
curl_close($ch2); 
?> 

Wenn ich den Code der Cookie-Datei erstellt ausführen, aber ich erhalte eine Fehlermeldung „Session verloren Bitte loggen Sie sich erneut ein ".

+0

http://stackoverflow.com/questions/13020404/keeping-session-alive-with-curl-and-php#13020494 – JustOnUnderMillions

+0

Ich habe schon gelesen, dass Frage vor, aber nichts Neues, können Sie genauer über das Problem meines Codes sein? –

+0

Thing sollten Sie nur einen dieser 'CURLOPT_COOKIEJAR | CURLOPT_COOKIEFILE'' CURLOPT_COOKIEJAR' im ersten Aufruf und 'CURLOPT_COOKIEFILE' im zweiten Teil http://stackoverflow.com/a/13020460/4916265 – JustOnUnderMillions

Antwort

0

Sie müssen einen Handle über curl_share_init erstellen und an jede cURL-Instanz übergeben, da die Instanzen andernfalls separat existieren und den erforderlichen Sitzungscookie nicht teilen können.

Ein Beispiel aus dem PHP-Handbuch:

// Create cURL share handle and set it to share cookie data 
$sh = curl_share_init(); 
curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); 

// Initialize the first cURL handle and assign the share handle to it 
$ch1 = curl_init("http://example.com/"); 
curl_setopt($ch1, CURLOPT_SHARE, $sh); 

// Execute the first cURL handle 
curl_exec($ch1); 

// Initialize the second cURL handle and assign the share handle to it 
$ch2 = curl_init("http://php.net/"); 
curl_setopt($ch2, CURLOPT_SHARE, $sh); 

// Execute the second cURL handle 
// all cookies from $ch1 handle are shared with $ch2 handle 
curl_exec($ch2); 

// Close the cURL share handle 
curl_share_close($sh); 

// Close the cURL handles 
curl_close($ch1); 
curl_close($ch2); 
+0

habe ich versucht wie gesagt aber es gibt immer noch den gleichen fehler, ich habe den code in der frage entsprechend aktualisiert, bitte schauen sie es sich an. –