[wp-trac] [WordPress Trac] #8727: WP_Http_Curl doesn't handle cookies

WordPress Trac wp-trac at lists.automattic.com
Fri Dec 26 16:51:52 GMT 2008


#8727: WP_Http_Curl doesn't handle cookies
-------------------------------+--------------------------------------------
 Reporter:  Denis-de-Bernardy  |       Owner:  anonymous
     Type:  defect (bug)       |      Status:  new      
 Priority:  normal             |   Milestone:  2.7.1    
Component:  Administration     |     Version:  2.7      
 Severity:  normal             |    Keywords:           
-------------------------------+--------------------------------------------
 WP_Http_Curl doesn't handle cookies properly, and I see no means to "fix
 things up" using a plugin hook.

 for information, note that some hosts fail when using curl_setop() for
 CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE.

 I worked around this in one of my own classes at one point. The code I
 used is the following:


 {{{
 curl_setopt($ch, CURLOPT_HEADERFUNCTION, array('sem_http',
 'curl_cookies'));

 }}}

 with:


 {{{
         #
         # curl_cookies()
         #
         # via http://www.php.net/manual/en/function.curl-setopt.php#70043
         #

         function curl_cookies($ch, $string)
         {
                 global $sem_curl_cookies;

                 # ^overrides the function param $ch
                 # this is okay because we need to
                 # update the global $ch with
                 # new cookies

                 $length = strlen($string);

                 if ( !strncmp($string, "Set-Cookie:", 11) )
                 {
                         # get the cookie
                         $cookiestr = trim(substr($string, 11, -1));
                         $cookie = explode(';', $cookiestr);
                         $cookie = explode('=', $cookie[0]);
                         $cookiename = trim(array_shift($cookie));
                         $sem_curl_cookies[$cookiename] = trim(implode('=',
 $cookie));
                 }

                 if ( trim($string) == "" )
                 {
                         $cookie = "";

                         foreach ( (array) $sem_curl_cookies as $key =>
 $value )
                         {
                                 $cookie .= "$key=$value; ";
                         }

                         $sem_curl_cookies = array();

                         #dump(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
 $cookie);

                         curl_setopt($ch, CURLOPT_COOKIE, $cookie);
                 }

                 return $length;
         } # curl_cookies()
 }}}

 a few remarks on the above, since WP has a broader user base than the
 handful of customers I distributed the above to:

  - you'll want to use a class variable instead of a global variable

  - strlen isn't multi-byte safe, so there might need some tweaking here

  - $cookie .= "$key=$value; "; may need some attention. I'm uncertain how
 well curl behaves when $key or $value contain strings that might need to
 be urlencoded.

 else it works fine...

-- 
Ticket URL: <http://trac.wordpress.org/ticket/8727>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list