@@ -114,8 +114,38 @@ public function getMethod(){
114114 return $ this ->method ;
115115 }
116116
117- public function setUrl ($ url ){
117+ // https://github.com/guzzle/psr7/blob/master/src/functions.php
118+ public static function parseQuery ($ query ){
119+
120+ $ result = array ();
121+
122+ foreach (explode ('& ' , $ query ) as $ kvp ){
123+ $ parts = explode ('= ' , $ kvp );
124+ $ key = rawurldecode ($ parts [0 ]);
125+ if (substr ($ key , -2 ) == '[] ' ){
126+ $ key = substr ($ key , 0 , -2 );
127+ }
128+
129+ // keys with NULL will be ignored in http_build_query - that's why it has to be ''
130+ $ value = isset ($ parts [1 ]) ? rawurldecode ($ parts [1 ]) : '' ;
131+
132+ // brand new key=value
133+ if (!isset ($ result [$ key ])){
134+ $ result [$ key ] = $ value ;
135+ } else {
136+ // key already exists in some form...
137+ if (!is_array ($ result [$ key ])){
138+ $ result [$ key ] = array ($ result [$ key ]);
139+ }
140+
141+ $ result [$ key ][] = $ value ;
142+ }
143+ }
144+
145+ return $ result ;
146+ }
118147
148+ public function setUrl ($ url ){
119149 // remove hashtag - preg_replace so we don't have to check for its existence first - is it possible preserving hashtag?
120150 $ url = preg_replace ('/#.*/ ' , '' , $ url );
121151
@@ -124,16 +154,15 @@ public function setUrl($url){
124154
125155 // remove it and add the query params to get collection
126156 if ($ query ){
127- $ url = str_replace ('? ' .$ query , '' , $ url );
128-
129- $ temp = array ();
130- mb_parse_str ($ query , $ temp );
157+ //$url = str_replace('?'.$query, '', $url);
158+ $ url = preg_replace ('/\?.*/ ' , '' , $ url );
131159
132- $ this ->get ->replace ($ temp );
160+ $ result = self ::parseQuery ($ query );
161+ $ this ->get ->replace ($ result );
133162 }
134163
164+ // url without query params - those will be appended later
135165 $ this ->url = $ url ;
136-
137166 $ this ->headers ->set ('host ' , parse_url ($ url , PHP_URL_HOST ));
138167 }
139168
0 commit comments