URL编码字符串就像Google在Chrome中所做的那样

时间:2015-09-27 13:31:47

标签: javascript php curl encoding

我有一个我需要在PHP中使用CURL的URL:

http://query.yahooapis.com/v1/public/yql?q= select * from yahoo.finance.historicaldata where symbol = "LINE" and startDate = "2015-09-23" and endDate  = "2015-09-25" &format=xml &env=store://datatables.org/alltableswithkeys

当我使用URL"按原样#34;我什么都没得到,没有数据,空白页。

当我在Chrome的地址栏中输入网址时,我会获得一些编码,网址如下所示:

http://query.yahooapis.com/v1/public/yql?q=%20select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20=%20%22LINE%22%20and%20startDate%20=%20%222015-09-23%22%20and%20endDate%20=%20%222015-09-25%22%20&format=xml%20&env=store://datatables.org/alltableswithkeys

现在,当我将上面的URL压缩到它上面时,我得到了数据。 问题是我需要执行此程序而不是复制/粘贴。所以我使用了rawurlencodeurlencode但他们没有像上面那样转换网址,我再次获得了空白页。

我应该使用哪种类型的编码(最好使用PHP但javascript也不错)来转换像Google Chrome一样的网址?

1 个答案:

答案 0 :(得分:0)

如果使用Javascript,您可以使用encodeURI

document.querySelector('div').textContent = encodeURI('http://query.yahooapis.com/v1/public/yql?q= select * from yahoo.finance.historicaldata where symbol = "LINE" and startDate = "2015-09-23" and endDate  = "2015-09-25" &format=xml &env=store://datatables.org/alltableswithkeys');
<div></div>