开发者

PHP CURLOPT_COOKIEFILE/CURLOPT_COOKIEJAR do not seem to be working on remote server

I have a rather interesting issue with passing cookies using curl. I have a script that logs into my company's queuing system, then grabs the cookies from the successful login. Later, the script will then recall those cookies to query the system and parse the output. The script works flawlessly on localhost, but migrating it to the company's server it does not. It is creating the cookie file, but all filesizes = 0 bytes. I'm thinking this is a permissions issue but I don't see how (I went so far as to chmod 777 the files). The weirdest thing is that I can use fwrite() to add to any of the files. I'm not really sure where to start debugging here so I'm hoping one of you might have an idea. Below are the relevant code snippets:

[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,     // 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_COOKIEFILE     => $ckfile,
            CURLOPT_POSTFIELDS     => "username=$user&usernamecheck=1" //derp

        );

        $ch      = curl_init( $url );开发者_如何学C
        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]

At this point, I'm thinking it may be a good idea to attempt to just parse the headers for the cookies and work with them....but that is less than ideal. Any suggestions are greatly appreciated :D

Thank you!


Greetings,

So I figured out what the issue was. I was opening the file with fopen(), then passing the file handler through to cURL. What I should have done is pass the full file path+name to cURL. Corrected portion of code:

$ckfile = dirname(__FILE__) . "/". sha1($username);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜