2013-10-10 18 views
6

私は、cookiejarにcookieを保存しようとしています。私はコードを単純化しましたが、機能しません。なぜphp curlがcookieをcookieに保存しないのですか?

<?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); 

?> 

受信したヘッダ

HTTP/1.1 200 OK日:木、2013年10月10日午前12時10分37秒GMTサーバー:Apacheの /2.4。 PHP/5.4.12セットクッキー: TestCookie = something + from + somewhere Content-Length:0コンテンツタイプ: text/html

したがって、testcookieはヘッダーにありますが、私のcookiefileは空のままです。私は間違っているの?私はこの例を動作させるために何を試すことができますか?ありがとうございました!

答えて

14

CURLOPT_COOKIEJARを設定する場合、絶対パスを使用する必要があります。あなたは使って簡単に行うことができます。

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

参考:cannot use cookies in cURL PHP

+0

はありがとうございました!私はこれで数時間前から苦労していましたが、絶対パスを試しても動作しませんでした。 realpathがトリックをしました。ありがとうございました。 –

+2

'realpath'は絶対パスを返しますので、あなたはこれに苦労していたときに何か間違っていました。 –

+3

CURLOPT_COOKIEJAR絶対パスはWindowsプラットフォームでのみ必要です。 Linux/Unixでは、パスは相対パスとすることができます。私はそれをチェックした。 –

関連する問題