php file_get_contents($url) & 变成 &

标签 php url file-get-contents url-encoding ampersand

我正在尝试像这样向 coinbase api 发出请求

$url = "https://api.gdax.com/products/BTC-USD/candles?start=".date($format,$starting_date)."&end=".date($format,$ending_date)."&granularity=".$granularity;

然后我在 file_get_contents($url) 中传递它,但它给了我一个错误

file_get_contents(https://api.gdax.com/products/BTC-USD/candles?start=2015-05-07&end=2015-05-08&granularity=900) : 无法打开流:HTTP 请求失败! HTTP/1.1 400 错误请求。

问题当然是什么时候“&”变成了“&”。

最佳答案

发布这个是因为它最适合我。我知道这是一个老问题。

我发现在使用 http_build_query 构建查询时,& 符号问题消失了。

例子

$url = 'https://example.url';

// http_build_query builds the query from an array
$query_array = array (
    'search' => $string,
    'from' => $from,
    'to' => $to,
    'format' => 'json'
);

$query = http_build_query($query_array);
$result = file_get_contents($url . '?' . $query);

编辑:刚看到@joshi 的回答(实际上是点击了链接),那里使用了 http_build_query。

关于php file_get_contents($url) & 变成 &,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30120211/

相关文章:

php - 在 JQuery AJAX POST 上...请求失败 : URI too long (longer than 8190)

php - jquery post() 不以动态形式工作

php - 简单的php登录系统

php - 如果出现$ Error,PHP将不显示网格

php - 将文件加载为包含变量定义的字符串

php - file_get_contents() 的等效函数?

php - CURL 仅在文件被修改时获取文件

java - Java 中的资源、URI、URL、路径和文件有什么区别?

java - URI 与 URL 与 URN

php - Safari中如何处理字符为 'é'的URL?