CURL und Session-Cookies

DataFox

Erfahrenes Mitglied
Hi Leute

ich suche eine gute Dokumentation von CURL mit Praxisbeispielen

aus http://de.php.net/manual/en/ref.curl.php werde ich nicht so richtig schlau...

das Problem: Ich hab ein Script gebastelt das als einfacher Proxy t um eine Website zu übersetzen. Session-Cookies funktionieren damit aber nicht, weil der Browser die nicht bekommt und somit klappt das Ganze nicht wenn man irgendwo eingeloggt ist.

Wie macht man das mit den Session-Cookies? Muss ich den originalen Response-Header einfach 1:1 an den Browser weitergeben, so das er die Cookie-Information bekommt und das Sessioncookie speichert? Was ich noch gar nicht verstehe: Wie kriegt man es hin, das die Website den Sessioncookie auslesen kann - im prinzip muss ja der Proxy den Cookie dann raus rücken, oder?

Gruß
Laura
 
Vielmals stehen Beispiele direkt in der Referenz unter Kommentare
http://ch2.php.net/curl

z.B.:
Luca hat gesagt.:
If you're getting trouble with cookie handling in curl:

- curl manages tranparently cookies in a single curl session
- the option
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");

makes curl to store the cookies in a file at the and of the curl session

- the option
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");

makes curl to use the given file as source for the cookies to send to the server.

so to handle correctly cookies between different curl session, the you have to do something like this:

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE_PATH);
curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE_PATH);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch);
curl_close($ch);
return $result;

in particular this is NECESSARY if you are using PEAR_SOAP libraries to build a webservice client over https and the remote server need to establish a session cookie. in fact each soap message is sent using a different curl session!!

I hope this can help someone
Luca
 
PHP:
 $ch = curl_init();

 $url="www2.htlwrn.ac.at/d05044/sbnk/buecher.php";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');


$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

if ($output === false || $info['http_code'] != 200) {
  $output = "No cURL data returned for $url [". $info['http_code']. "]";
  if (curl_error($ch))
    $output .= "\n". curl_error($ch);
  }
else {
  // 'OK' status; format $output data if necessary here:
  echo 'Hat funktioniert!';
  echo $output;
}

// then return or display the single string $output

Was stimmt an diesem code nicht?
Ich will das Cookie auslesen und die in die cookies.txt speichern!
Jedoch ist die datei immer leer!

Danke schon im voraus!
 
Ich war in der Lage die Cookies zu lesen. Hatte div. Probleme mit Rechten bei 'cookies.txt', also lese ich aus dem Header heraus.

PHP:
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch,CURLOPT_COOKIEJAR,"cookies.txt");
	
	curl_setopt($ch, CURLOPT_HEADER, 1);
	
	$output = curl_exec($ch);
	$info = curl_getinfo($ch);
	
	
	$header = $output;
	$end = strpos($header, "Content-Type");
	$start = strpos($header, "Set-Cookie");
	$parts = split("Set-Cookie: ",substr($header, $start, $end-$start));
	$cookies = array();
	foreach ($parts as $co)
	{
	$cd = split(";",$co);
	if (!empty($cd[0]))
	$cookies[] = $cd[0];
	}
	
	//file_put_contents ("cookies.txt", "test"); 
	
	if ($output == false /*|| $info['http_code'] != 200*/) {
	  $output = "No cURL data returned for $url [". $info['http_code']. "]<br/>";
	  if (curl_error($ch))
		$output .= "\n". curl_error($ch);
	   echo 'Es ist ein Fehler aufgetreten!<br/>';
	  // echo $output;
	   echo implode(";",$cookies);
	   file_put_contents("cookies.txt",implode(";",$cookies));
	  }
	else {
	  // 'OK' status; format $output data if necessary here:
	  echo 'Hat funktioniert!<br/>';
	  //echo $output;
	  echo implode(";",$cookies);
	  echo $output;
	  file_put_contents("cookies.txt",implode(";",$cookies));
	}
	curl_close($ch);

Nun ergibt sich bei mir folgendes Problem: Da ich ja das Cookie (endlich) habe, verschicke ich 2 Post-Variablen und bekomme immer wieder folgende Meldung zurück:


Die Seite kann nicht angezeigt werden
Es liegt ein Problem mit der Seite vor, auf die Sie zuzugreifen versuchen, und deshalb kann sie nicht angezeigt werden.

Versuchen Sie Folgendes:

* Klicken Sie auf die Schaltfläche Aktualisieren, oder versuchen Sie es später noch einmal.
* Öffnen Sie localhost die Homepage, und suchen Sie dann nach den Verknüpfungen zu den gewünschten Informationen.

HTTP 500.100 - Interner Serverfehler - ASP-Fehler
Internet-Informationsdienste

Technische Informationen (für den Support)

* Fehlertyp:
Laufzeitfehler in Microsoft VBScript (0x800A01F5)
Ungültige Zuweisung: 'TConn'
/const.inc, line 30

* Browsertyp:

* Seite:
POST 28 bytes to /results.asp

* POST Data:
Stichwort=aloha&ZST=31818001

* Zeit:
Montag, 6. April 2009, 10:14:55

* Weitere Informationen:
Microsoft Support


Mein Code:
PHP:
echo $url."<br/>";

	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');

	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,120);
	curl_setopt($ch, CURLOPT_LOW_SPEED_LIMIT,   61440);
    curl_setopt($ch, CURLOPT_LOW_SPEED_TIME,    20); 
	curl_setopt($ch,CURLOPT_TIMEOUT,6580);
	curl_setopt($ch,CURLOPT_POST,1);
	curl_setopt($ch,CURLOPT_POSTFIELDS,"Stichwort=".$_POST['stichwort']."&ZST=31818001");
	
	curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
	
	$output = curl_exec($ch);
	$info = curl_getinfo($ch);

	if ($output == false /*|| $info['http_code'] != 200*/) {
		  $output = "No cURL data returned for $url [". $info['http_code']. "]<br/>";
		  if (curl_error($ch))
			$output .= "\n". curl_error($ch);
		   echo 'Es ist ein Fehler aufgetreten!<br/>';
		  // echo $output;
	 }
	else {
	  // 'OK' status; format $output data if necessary here:
	  echo 'Hat funktioniert!<br/>';
	  echo $output;
	}

Wenn ich die Cookie-Option weglasse bekomme ich den richtigen Fehler, also kann es daran nicht liegen.
Ist es also ein serverseitiges Problem?

Mfg
fefi
 

Neue Beiträge

Zurück