@@ -167,4 +167,53 @@ public function is_equal_to($b)
167167 {
168168 return ($ this ->method === $ b ->method ) && ($ this ->body === $ b ->body ) && ($ this ->headers === $ b ->headers );
169169 }
170+
171+ /**
172+ * Generate a URL for the hurl.it service.
173+ *
174+ * @param string $base_url URL to the base server
175+ * @param array $additional Extra options to pass to the service
176+ *
177+ * @return string
178+ */
179+ public function get_hurl_link ($ base_url , $ additional = [])
180+ {
181+ $ options = [];
182+
183+ $ type = (isset ($ this ->headers ['Content-Type ' ])) ? $ this ->headers ['Content-Type ' ] : NULL ;
184+
185+ $ url = $ this ->parent ->build_url ($ base_url , TRUE );
186+ $ url = explode ('? ' , $ url );
187+ if (isset ($ url [1 ])) {
188+ $ params = [];
189+ foreach (explode ('& ' , $ url [1 ]) as $ args ) {
190+ $ arg = explode ('= ' , $ args );
191+ $ params [$ arg [0 ]] = [$ arg [1 ]];
192+ }
193+ $ options [] = 'args= ' . json_encode ($ params );
194+ }
195+ $ options [] = 'url= ' . $ url [0 ];
196+ $ options [] = 'method= ' . strtoupper ($ this ->method );
197+ if (empty ($ this ->body )) {
198+ //NO-OP
199+ } elseif (is_string ($ this ->body )) {
200+ $ options [] = 'body= ' . urlencode ($ this ->body );
201+ } elseif (is_array ($ this ->body )) {
202+ $ options [] = 'body= ' . urlencode (join ('' , $ this ->body ));
203+ } elseif (is_subclass_of ($ this ->struct , StructureElement::class)) {
204+ foreach ($ this ->struct ->value as $ body ) {
205+ $ options [] = 'body= ' . urlencode (strip_tags ($ body ->print_request ($ type )));
206+ }
207+ }
208+ $ headers = [];
209+ if (!empty ($ this ->headers )) {
210+ foreach ($ this ->headers as $ header => $ value ) {
211+ $ headers [$ header ] = [$ value ];
212+ }
213+ $ options [] = 'headers= ' . json_encode ($ headers );
214+ }
215+ $ options = array_merge ($ options , $ additional );
216+
217+ return str_replace ('" ' , '\" ' , 'https://www.hurl.it/? ' . join ('& ' , $ options ));
218+ }
170219}
0 commit comments