php file_get_contents($url) & turns into & -


i trying make request coinbase api this

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

and pass in file_get_contents($url) gives me error

file_get_contents(https://api.gdax.com/products/btc-usd/candles?start=2015-05-07&end=2015-05-08&granularity=900): failed open stream: http request failed! http/1.1 400 bad request.

the problem of course when '&' gets changed '&'.

it seems need define user agent. try this;

$url = "https://api.gdax.com/products/btc-usd/candles?start=2015-05-07&end=2015-05-08&granularity=900";  $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch,curlopt_useragent,"mozilla/5.0 (windows; u; windows nt 5.1; en-us; rv:1.8.1.13) gecko/20080311 firefox/2.0.0.13"); curl_setopt($ch, curlopt_returntransfer, true); $result = curl_exec($ch); curl_close($ch);  var_dump($result); 

if still insist on using file_get_contents still possible use user agent;

$url = "https://api.gdax.com/products/btc-usd/candles?start=2015-05-07&end=2015-05-08&granularity=900";  $options = array(     "http"=>array(         "header"=>"user-agent: mozilla/5.0 (ipad; u; cpu os 3_2 mac os x; en-us) applewebkit/531.21.10 (khtml, gecko) version/4.0.4 mobile/7b334b safari/531.21.102011-10-16 20:23:10\r\n" // i.e. ipad     ) );  $context = stream_context_create($options); $result = file_get_contents($url, false, $context); var_dump($result); 

for more information can check file_get_contents , stream_context_create (for using headers) documentation


Comments