PHP Curl和setcookie问题

编程入门 行业动态 更新时间:2024-10-17 05:33:15
本文介绍了PHP Curl和setcookie问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个curl脚本,作为客户端和主服务器之间的代理。

I have a curl script that acts as proxy between client and main server.

......

$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;

现在,在主服务器上,我在脚本中设置了一个cookie,然后尝试读取它的值另一个脚本。我不能读取值。所以有一些问题在curl中传递值。如何解决?

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?

感谢

其实,一个愚蠢的问题。我需要在CURLOPT_COOKIE中显式设置Cookie。以下代码现在适用于我:

Actually, a stupid problem. I need to explicitly set cookies in CURLOPT_COOKIE. Following code now works for me:

...... $_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); ......

推荐答案

必须在分号后添加空格,因此最好的方法是:

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);

更多推荐

PHP Curl和setcookie问题

本文发布于:2023-06-06 00:34:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/531164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   Curl   setcookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!