URL编码字符串的方式,谷歌在Chrome浏览器


URL encode string the way google does in Chrome

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

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 "as is"时,我什么也得不到,没有数据,空白页面。

当我在Chrome的地址栏中输入URL时,我得到一些编码,URL看起来像这样:

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

现在,当我CURL这个URL上面的工作,我得到数据。问题是,我需要做这个程序化的,而不是复制/粘贴。所以我使用了rawurlencodeurlencode,但是它们不像上面那样转换URL,我再次得到一个空白页面。

什么类型的编码(最好是PHP,但javascript也很好),我应该使用转换URL像谷歌Chrome浏览器吗?

如果使用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>