PHP CURLOPT_COOKIEFILE/CURLOPT_COOKIEJAR 似乎无法在远程服务器上工作
我在使用curl传递cookie时遇到了一个相当有趣的问题。我有一个脚本可以登录我公司的排队系统,然后从成功登录中获取 cookie。稍后,脚本将调用这些 cookie 来查询系统并解析输出。该脚本在本地主机上可以完美运行,但将其迁移到公司的服务器时却无法正常运行。它正在创建 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, // 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
谢谢!
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 );
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您好,
所以我弄清楚了问题所在。我使用 fopen() 打开文件,然后将文件处理程序传递给 cURL。我应该做的是将完整文件路径+名称传递给 cURL。更正的代码部分:
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: