Ver Mensaje Individual
  #7 (permalink)  
Antiguo 28/08/2010, 18:26
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Problema con PHP, cURL y cookies. No puedo hacer una segunda llamada.

Se supone que funcione correctamente como lo estás indicando. Pero para que tengas una mejor idea de lo que puedes hacer:
a.php
Código PHP:
Ver original
  1. <?php
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, 'http://localhost/b.php');
  4. curl_setopt($ch, CURLOPT_POST, true);
  5. curl_setopt($ch, CURLOPT_POSTFIELDS, 'foo=bar&baz=candy');
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  7. curl_setopt($ch, CURLOPT_COOKIEFILE, getcwd() . '/cookie.txt');
  8. curl_setopt($ch, CURLOPT_COOKIEJAR, getcwd() . '/cookie.txt');
  9. $page = curl_exec($ch);
  10.  
  11. echo '<pre>';
  12. var_dump($page);
  13. echo '</pre>';
  14.  
  15.  
  16. $ch = curl_init();
  17. curl_setopt($ch, CURLOPT_URL, 'http://localhost/c.php');
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  19. curl_setopt($ch, CURLOPT_COOKIEFILE, getcwd() . '/cookie.txt');
  20. curl_setopt($ch, CURLOPT_COOKIEJAR, getcwd() . '/cookie.txt');
  21. $page = curl_exec($ch);
  22.  
  23. echo '<pre>';
  24. var_dump($page);
  25. echo '</pre>';
  26.  
  27.  
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_URL, 'http://localhost/c.php');
  30. curl_setopt($ch, CURLOPT_COOKIE, 'foo=vegetable; baz=fuits');
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  32. $page = curl_exec($ch);
  33.  
  34. echo '<pre>';
  35. var_dump($page);
  36. echo '</pre>';

b.php
Código PHP:
Ver original
  1. <?php
  2. setcookie('foo', $_POST['foo']);
  3. setcookie('baz', $_POST['baz']);
  4.  
  5. var_dump($_POST);

c.php
Código PHP:
Ver original
  1. <?php
  2. var_dump($_COOKIE);

Resultado
Código HTML:
Ver original
  1. string(13) "array(0) {
  2. }
  3. "
  4.  
  5. string(75) "array(2) {
  6.   ["baz"]=>
  7.   string(5) "candy"
  8.   ["foo"]=>
  9.   string(3) "bar"
  10. }
  11. "
  12.  
  13. string(81) "array(2) {
  14.   ["foo"]=>
  15.   string(9) "vegetable"
  16.   ["baz"]=>
  17.   string(5) "fuits"
  18. }
  19. "

Resultado en cookie.txt
Código txt:
Ver original
  1. # Netscape HTTP Cookie File
  2. # http://curl.haxx.se/rfc/cookie_spec.html
  3. # This file was generated by libcurl! Edit at your own risk.
  4.  
  5. localhost   FALSE   /   FALSE   0   foo bar
  6. localhost   FALSE   /   FALSE   0   baz candy

Si te fijas, puedes enviar directamente las cookies con CURLOPT_COOKIE. En el área de aportes hay un tema que da más ejemplos.
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos