【问题标题】:URL encode string the way google does in ChromeURL 编码字符串就像谷歌在 Chrome 中所做的那样
【发布时间】:2015-12-24 20:04:27
【问题描述】:

我有一个需要在 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 时,我什么也得不到,没有数据,空白页。

当我在 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

现在,当我在它上面卷曲这个 URL 时,我得到了数据。 问题是我需要以编程方式而不是复制/粘贴。所以我使用了rawurlencodeurlencode 但它们并没有像上面那样转换URL,我又得到了一个空白页。

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

【问题讨论】:

  • 所以我使用了 rawurlencode 和 urlencode - 你是不是只在查询字符串的参数上应用了它?
  • @Leggendario 我在整个 URL 上使用了它,但没有用,然后我只在参数上使用它,也没有用。
  • 我在整个 URL 上都使用了它 - 这是一个错误。 然后我只在参数上使用它 - 你是怎么做到的?
  • @Leggendario 没关系。有很多参数,我必须把它分成十块左右。我正在寻找一种更通用的方法来获取整个 URL。问题(Ithink)是双引号和等号也被编码,这就是产生问题的原因
  • 如果您说“没关系”,我们如何回答您?让我们看看你如何编码你的 uri。你可能想使用这样的东西:stackoverflow.com/a/6059053/711206

标签: javascript php curl encoding


【解决方案1】:

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

【讨论】:

  • 非常感谢:) 关于如何在 PHP 中执行此操作的任何想法?
  • 我在这里做一些研究,但我从未使用过php,对不起:(
  • @user2604269 看看这种方法:stackoverflow.com/questions/4929584/encodeuri-in-php - 给出更多投票答案的人在 PHP 中创建了一个辅助函数,其行为与 Javascript 的 encodeURI 相同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
  • 2014-09-08
  • 2023-02-16
相关资源
最近更新 更多