我正在尝试像这样向 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/