PHP CURLOPT_COOKIEFILE / CURLOPT_COOKIEJAR在远程服务器上似乎不起作用

问题描述

| 我在使用curl传递cookie时遇到一个相当有趣的问题。我有一个脚本登录到我公司的排队系统,然后从成功登录获取cookie。稍后,脚本将重新调用这些cookie,以查询系统并解析输出。该脚本可在localhost上完美运行,但不能将其迁移到公司的服务器上。它正在创建cookie文件,但是所有文件大小= 0个字节。我认为这是一个权限问题,但我不知道如何解决(我对chmod 777文件进行了修改)。最奇怪的是,我可以使用fwrite()添加到任何文件中。我不确定在哪里开始调试,所以我希望你们当中的一个有想法。以下是相关的代码段:
[Syntax=PHP]

main class{

include_once(\'../shared_data/cquserdata.PHP\');
$ckfile_name = tempnam(\'temp/\',sha1(\'CqAuth\'));
$ckfile = fopen($ckfile_name,\'w\') or die(\'Derp...open...nooooooooo!\');

cqUserData::cqLogin($credentials[\'username\'],$credentials[\'pass\'],$ckfile);

(skip a few thousand lines)
$puname = sanitizers::sanitize($_POST[\'puname\']); //sanitize post input
$manager = cqUserData::getManager($puname,$ckfile);


}

cqLogin($username,$pass,$ckfile){

$url = \'URL\';

        $options = array(

            CURLOPT_RETURNTRANSFER => true,// return web page
            CURLOPT_HEADER         => false,// don\'t return headers
            CURLOPT_FOLLOWLOCATION => true,// follow redirects
            CURLOPT_ENCODING       => \"\",// handle all encodings
            CURLOPT_AUTOREFERER    => true,// set referer on redirect
            CURLOPT_CONNECTTIMEOUT => 120,// timeout on connect
            CURLOPT_TIMEOUT        => 120,// timeout on response
            CURLOPT_MAXREDirs      => 10,// stop after 10 redirects
            CURLOPT_SSL_VERIFYHOST => 0,CURLOPT_SSL_VERIFYPEER => 0,CURLOPT_USERAGENT      => \'uuberness\',CURLOPT_COOKIEJAR     => $ckfile,CURLOPT_POSTFIELDS     => \"redirect=&username=$username&password=$pass\" //derp

        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch,$options );
        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );
        curl_close( $ch );

        return true;

}

getManager($user,$ckfile){

$url = \"URL\"\';

        $options = array(

            CURLOPT_RETURNTRANSFER => true,CURLOPT_COOKIEFILE     => $ckfile,CURLOPT_POSTFIELDS     => \"username=$user&usernamecheck=1\" //derp

        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch,$options );

        $content = curl_exec( $ch );
        $err     = curl_errno( $ch );
        $errmsg  = curl_error( $ch );
        $header  = curl_getinfo( $ch );

        curl_close( $ch );

        $header[\'errno\']   = $err;
        $header[\'errmsg\']  = $errmsg;
        $header[\'content\'] = $content;
        $header[\'data\'] = $data;

        $doc = new DOMDocument;

        $doc->loadHTML($content);
        $doc->preserveWhiteSpace = false;
        $tables = $doc->getElementsByTagName(\'table\');

        foreach($tables as $table){

            ***Code excluded,loops rows to find correct,then defines manager**

            $manager = $manager[1];


        }

        return $manager;

}

[/Syntax]
在这一点上,我认为尝试解析cookie的标头并使用它们可能是个好主意。但是,这并不理想。任何建议,不胜感激:D 谢谢!     

解决方法

问候, 所以我弄清楚了问题所在。我使用fopen()打开文件,然后将文件处理程序传递给cURL。我应该做的是将完整的文件路径和名称传递给cURL。更正的代码部分:
$ckfile = dirname(__FILE__) . \"/\". sha1($username);