2013-10-10 13 views
6

Sto provando a salvare un cookie nella curl cookiejar. Ho semplificato il mio codice ma non funziona.Perché php curl non salva i cookie nel mio cookiefile?

<?php 

$cookie_file = './cookies.txt'; 

if (! file_exists($cookie_file) || ! is_writable($cookie_file)){ 
    echo 'Cookie file missing or not writable.'; 
    exit; 
}//cookie_file is writable, so this is not the issue 

$ch = curl_init (html_entity_decode("http://localhost/kiala_test/setcookie.php")); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$output = curl_exec ($ch); 
echo $output; 
?> 

setcookie.php

<?php 
$value = 'something from somewhere'; 
setcookie("TestCookie", $value); 

?> 

intestazione ricevuto

HTTP/1.1 200 OK Data: Giovedì, 10 ottobre 2013 12:10:37 GMT Server: Apache/2.4. 4 (Win64) PHP/5.4.12 X-Powered-By: PHP/5.4.12 Set-Cookie: TestCookie = qualcosa + da + qualche parte Content-Length: 0 Content-Type: text/html

quindi il testcookie è nell'intestazione ma il mio cookiefile rimane vuoto. Cosa sto facendo di sbagliato? cosa posso provare a far funzionare questo esempio? grazie!

risposta

14

Quando si imposta CURLOPT_COOKIEJAR, è necessario utilizzare un percorso assoluto. È possibile farlo facilmente utilizzando:

curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($cookie_file)); 

Riferimento: cannot use cookies in cURL PHP

+0

grazie! Ho lottato con questo per qualche ora, ho persino provato percorsi assoluti ma non funzionava. realpath ha fatto il trucco. grazie ancora. –

+2

'realpath' restituisce il percorso assoluto, quindi stavi facendo qualcosa di sbagliato, quando stavi lottando con questo. –

+3

Il percorso assoluto CURLOPT_COOKIEJAR è richiesto solo per la piattaforma Windows. Nel percorso Linux/Unix può essere relativo. L'ho controllato. –

Problemi correlati