From 6b985926f792d28f5e6ff98c001996dfbac83ef3 Mon Sep 17 00:00:00 2001 From: Zetao Yang Date: Thu, 14 Sep 2017 19:20:25 +0800 Subject: [PATCH] add del_cookie function --- core/requests.php | 66 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/core/requests.php b/core/requests.php index cd845de..183b2b3 100644 --- a/core/requests.php +++ b/core/requests.php @@ -194,6 +194,43 @@ public static function get_cookies($domain = '') return empty($domain) ? self::$cookies : self::$domain_cookies[$domain]; } + /** + * 删除Cookie + * + * @param string $domain 不传则删除全局Cookie + * @return void + * @author seatle + * @created time :2017-08-03 18:06 + */ + public static function del_cookie($key, $domain = '') + { + if (empty($key)) + { + return false; + } + + if (!empty($domain) && !isset(self::$domain_cookies[$domain])) + { + return false; + } + + if (!empty($domain)) + { + if (isset(self::$domain_cookies[$domain][$key])) + { + unset(self::$domain_cookies[$domain][$key]); + } + } + else + { + if (isset(self::$cookies[$key])) + { + unset(self::$cookies[$key]); + } + } + return true; + } + /** * 删除Cookie * @@ -208,13 +245,16 @@ public static function del_cookies($domain = '') { return false; } - if ( empty($domain)) + if ( empty($domain) ) { - unset(self::$cookies); + self::$cookies = array(); } else { - unset(self::$domain_cookies[$domain]); + if (isset(self::$domain_cookies[$domain])) + { + unset(self::$domain_cookies[$domain]); + } } return true; } @@ -640,22 +680,26 @@ public static function request($url, $method = 'GET', $fields = array(), $files if (!empty($fields)) { // 不是上传文件的,用http_build_query, 能实现更好的兼容性,更小的请求数据包 - if (is_array($fields) && empty($file_fields)) + if ( empty($file_fields) ) { - $fields = http_build_query($fields); + // post方式 + if ( is_array($fields) ) + { + $fields = http_build_query($fields); + } } else { - // 如果是json - if (!is_null($stance = json_decode($fields, true))) - { - $fields = json_encode(array_merge($stance, $file_fields)); - } - else + // 有post数据 + if ( is_array($fields) && !empty($fields) ) { // 某些server可能会有问题 $fields = array_merge($fields, $file_fields); } + else + { + $fields = $file_fields; + } } // 不能直接传数组,不知道是什么Bug,会非常慢 curl_setopt( self::$ch, CURLOPT_POSTFIELDS, $fields );