PHP - 使用cURL将cookie会话存储到变量/内存中 [英] PHP - Using cURL to store cookie session into variable / memory
问题描述
我试图避免cURL通过CURLOPT_COOKIEJAR将cookie会话存储到实际文件中。所以我创建了一个方法来捕获/解析cookie到一个局部变量 - 然后通过CURLOPT_COOKIE来恢复cookie会话。
I'm trying to avoid cURL storing the cookie session into an actual file via "CURLOPT_COOKIEJAR". So I created a method to catch / parse the cookies into a local variable - which is then used via "CURLOPT_COOKIE" to restore the cookie session.
通过
preg_match_all("/^Set-cookie: (.*?);/ism", $header, $cookies);
要使用CURLOPT_COOKIE,我们取key = value,并通过;分隔。但是(我知道),CURLOPT_COOKIE不允许你抛出各种标志。到期,安全标志等。
To use "CURLOPT_COOKIE" we take the key=value and separate them via "; ". However (As I'm aware), CURLOPT_COOKIE doesn't allow you throw in various flags I.e. expiration, secure flag, and so on.
Update 1/29/2014 6:45 pm
问题实际发生在CURLOPT_FOLLOWLOCATION发生的地方。我不认为它与标志有关。它似乎不像手动cookie会话,我有更新时,当一个新的位置(即一个网站有2-3重定向附加各种cookie /会话)。这实际上是有意义的,因为利用CURLOPT_COOKIEJAR将直接抓取/更新在标题重定向发送的cookie。所以,我试图创建一个手动重定向路径,同时抓取/附加最新的cookie - 但是这种方法不工作的一些明确的原因。
So I think my issue actually occurs where CURLOPT_FOLLOWLOCATION occurs. I don't think it has to do with the flags. It doesn't seem like the manual cookie session I have is updating when following a new location (i.e. a site has 2-3 redirects to append various cookies / session). Which would actually make sense because utilizing CURLOPT_COOKIEJAR will directly grab / update cookies sent on header redirects. So, I tried creating a manual redirection path while grabbing / appending the latest cookie - however this method did not work for some plain reason.
更新1/30 / 2014 4:22 pm
几乎有了这个想法。将很快更新答案。
Almost got this figured out. Will be updating with answer shortly. It turns out my method works perfectly fine, it's just a matter of jumping through the manual redirected pages correctly.
Update 1/30/2014 4:51 pm
Update 1/30/2014 4:51pm Issue solved -- answered myself below.
推荐答案
和我的假设是正确的。
-
将Cookie会话保存在变量中(与CURLOPT_COOKIEJAR相比)。 *请确保您已启用CURLOPT_HEADER和CURLINFO_HEADER_OUT。*
To keep the cookie session in a variable (vs. CURLOPT_COOKIEJAR). *Make sure you have CURLOPT_HEADER and CURLINFO_HEADER_OUT enabled.*
CURLOPT_FOLLOWLOCATION必须设置为false。否则您的Cookie将无法正确发送(这是CURLOPT_COOKIEJAR最好的地方)。
CURLOPT_FOLLOWLOCATION must be set to false. Otherwise your cookie won't send correctly (This is where CURLOPT_COOKIEJAR does best).
使用preg_match_all提取Cookie。然后使用strpos找到第一次出现的=。一些网站使用编码,并包含=的,将无法使用爆炸。
Use preg_match_all to extract cookies. Then use strpos to find the first occurence of "=". Some sites use encoding and include "="'s which won't work with "explode".
$data = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($data, 0, $header_size);
preg_match_all("/^Set-cookie: (.*?);/ism", $header, $cookies);
foreach( $cookies[1] as $cookie ){
$buffer_explode = strpos($cookie, "=");
$this->cookies[ substr($cookie,0,$buffer_explode) ] = substr($cookie,$buffer_explode+1);
}
进行下一个curl调用时, / object into CURLOPT_COOKIE。
When making your next curl call, re-call the cookie var/object into CURLOPT_COOKIE.
if( count($this->cookies) > 0 ){
$cookieBuffer = array();
foreach( $this->cookies as $k=>$c ) $cookieBuffer[] = "$k=$c";
curl_setopt($curl, CURLOPT_COOKIE, implode("; ",$cookieBuffer) );
}
希望这能帮助任何碰到这个问题的人!
Hope this helps anyone who bumps into this issue!
这篇关于PHP - 使用cURL将cookie会话存储到变量/内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!