PHP Curl 和 setcookie 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6453347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Curl and setcookie problem
提问by tanon
I have a curl script that acts as proxy between client and main server.
我有一个 curl 脚本,它充当客户端和主服务器之间的代理。
......
......
$field_array= array(
'Accept' => 'HTTP_ACCEPT',
'Accept-Charset' => 'HTTP_ACCEPT_CHARSET',
'Accept-Encoding' => 'HTTP_ACCEPT_ENCODING',
'Accept-Language' => 'HTTP_ACCEPT_LANGUAGE',
'Connection' => 'HTTP_CONNECTION',
'Host' => 'HTTP_HOST',
'Referer' => 'HTTP_REFERER',
'User-Agent' => 'HTTP_USER_AGENT'
);
$curl_request_headers=array();
foreach ($field_array as $key => $value) {
if(isset($_SERVER["$value"])) {
$server_value=$_SERVER["$value"];
$curl_request_headers[]="$key: $server_value";
}
};
$curl_request_headers[]="Expect: ";
session_write_close();
//Open connection
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_COOKIE,session_name()."=".session_id().";");
//Set the url, POST data
curl_setopt($curl_handle, CURLOPT_URL, $curl_url);
curl_setopt($curl_handle, CURLOPT_POST, !empty($user_post_data));
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $user_post_data);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $curl_request_headers);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl_handle);
//Close connection
curl_close($curl_handle);
list($headers,$content)=explode("\r\n\r\n",$result,2);
foreach (explode("\r\n",$headers) as $hdr) {
if(preg_match("/Transfer-Encoding:.*chunked/i", $hdr)) {
// Remove chunked headers. Not properly handled by browsers
} else {
header($hdr);
};
}
echo $content;
Now, on main server, I set a cookie in an script and then try to read its value in another script. I cannot read the value. So there is some problem passing the value around in curl. How to fix?
现在,在主服务器上,我在脚本中设置了一个 cookie,然后尝试在另一个脚本中读取它的值。我无法读取值。所以在 curl 中传递值存在一些问题。怎么修?
thanks
谢谢
FOUND SOLUTION:
找到的解决方案:
Actually, a stupid problem. I need to explicitly set cookies in CURLOPT_COOKIE. Following code now works for me:
其实是个蠢问题。我需要在 CURLOPT_COOKIE 中明确设置 cookie。以下代码现在对我有用:
......
$_COOKIE[session_name()]=session_id();
$cookie_string="";
foreach( $_COOKIE as $key => $value ) {
$cookie_string .= "$key=$value;";
};
//Open connection
$curl_handle = curl_init();
curl_setopt($curl_handle,CURLOPT_COOKIE, $cookie_string);
......
回答by Igor Vizma
you have to add space after semicolon so the best way is:
您必须在分号后添加空格,因此最好的方法是:
$cookie = array();
foreach( $_COOKIE as $key => $value ) {
$cookie[] = "{$key}={$value}";
};
$cookie = implode('; ', $cookie);
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_COOKIE, $cookie);
回答by Athlan
Consider http_build_cookie()instead of imploding cookies string or, if you have no pecl_http installed just:
考虑使用http_build_cookie()而不是内爆 cookie 字符串,或者,如果您没有安装 pecl_http:
http_build_query($cookies, null, ';')
Pretty simple.
很简单。