简体   繁体   English

使用特殊字符进行API调用

[英]Make API call with special characters

I have built an API. 我已经建立了一个API。 To perform a search on it, I do something like the following 要对其进行搜索,我需要执行以下操作

$tableData = DB::table($table)->where('search', $search)->get();

This will essentially form a URL like so (with table and search replaced by other values). 这实际上将形成一个这样的URL(用其他值替换表格和搜索)。

https://something.com/api/returnSearch/{{table}}/{{search}}

My problem is this. 我的问题是这个。 The search item it something along the lines of the following 搜索项目遵循以下内容

something (asda/tesco) 某些东西(asda / tesco)

So the complete URL looks like this 因此完整的网址如下所示

https://something.com/api/returnSearch/someTable/something (asda/tesco)

The problem is the forward slash in the search, seperating asda and tesco. 问题在于搜索中的正斜杠,将asda和tesco分开。 When I test it out in postman I get 当我在邮递员那里测试时,我得到

NotFoundHttpException in RouteCollection.php line 161: RouteCollection.php第161行中的NotFoundHttpException:

So I presume it thinks the forward slash is part of the url. 因此,我认为它认为正斜杠是URL的一部分。 Is there any way I can avoid this whilst still allowing a search with a forward slash? 有什么办法可以避免这种情况,同时仍然允许使用正斜杠进行搜索?

Thanks 谢谢

To include characters in a URL which have a special meaning in a URL , you need to escape/encode them according to URL formatting rules. 要在URL 中包含具有特殊含义的字符,您需要根据URL格式规则对它们进行转义/编码。 Specifically: rawurlencode . 具体来说: rawurlencode

printf('https://something.com/api/returnSearch/%s/%s',
       rawurlencode($table), rawurlencode($search))

// https://something.com/api/returnSearch/someTable/something%20%28asda%2Ftesco%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM