我使用curl传递cookie时遇到了一个相当有趣的问题。我有一个脚本登录到我公司的排队系统,然后从成功登录中获取cookie。之后,脚本将调用这些cookie来查询系统并解析输出。该脚本在localhost上完美运行,但将其迁移到公司的服务器却没有。它正在创建cookie文件,但所有filesizes = 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, // 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 );
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
谢谢!
答案 0 :(得分:1)
问候,
所以我弄清楚问题是什么。我用fopen()打开文件,然后将文件处理程序传递给cURL。我应该做的是将完整的文件路径+名称传递给cURL。更正了代码部分:
$ckfile = dirname(__FILE__) . "/". sha1($username);