php file_get_contents($url) & turns into &
Asked Answered
P

6

13

I am trying to make a request to the coinbase api like this

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

and then I pass that in file_get_contents($url) but it gives me an error

file_get_contents(https://api.gdax.com/products/BTC-USD/candles?start=2015-05-07&end=2015-05-08&granularity=900): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request.

The problem of course is when the '&' gets changed into '&'.

Polycarp answered 8/5, 2015 at 9:21 Comment(4)
Are you sure this is the problem? since going directly to that url works fine.Future
Maybe not. but what else could be the reason that it isn't returning the file contents and instead an error.Polycarp
this might answer your question : #3710647Future
There seems to be a bug in PHP 7.0 causing this for valid URL strings. Switching to 7.1 resolves it.Talus
E
3

It seems you need define an 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 you still insist on using file_get_contents then it is still possible to 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 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad
    )
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

For more information you can check file_get_contents and stream_context_create (for using headers) documentation

Erdah answered 8/5, 2015 at 9:39 Comment(6)
it dumped bool(false)Polycarp
Then you have a different problem. It's OK with me. I get results with both cURL and file_get_contentsNorthwester
it didn't work with curl, but the edited file_get_contents,WORKED. but could you please describe what is happening.Polycarp
The API expects User Agent to be set without setting an user agent, it simply doesn't work. I simply set user agent in order API to output a result with given parameters.Northwester
Ok. thats great. Thanks a million!Polycarp
My pleasure, I wish you best of luck with your projects.Northwester
V
7

Posting this because it worked best for me. I know it is an old question.

I found that when building the query using http_build_query the ampersand problem went away.

Example

$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);

edit: just saw @joshi s answer (actually followed the link), and http_build_query is used there.

Viosterol answered 12/9, 2017 at 9:38 Comment(0)
V
5

This can happen when one of the parameters you send is not urlencoded.

When php sees malformed url (e,g: with spaces or other url blacklisted characters) it urlencode's the full request.

Vacla answered 2/7, 2018 at 15:8 Comment(0)
E
3

It seems you need define an 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 you still insist on using file_get_contents then it is still possible to 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 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n" // i.e. An iPad
    )
);

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

For more information you can check file_get_contents and stream_context_create (for using headers) documentation

Erdah answered 8/5, 2015 at 9:39 Comment(6)
it dumped bool(false)Polycarp
Then you have a different problem. It's OK with me. I get results with both cURL and file_get_contentsNorthwester
it didn't work with curl, but the edited file_get_contents,WORKED. but could you please describe what is happening.Polycarp
The API expects User Agent to be set without setting an user agent, it simply doesn't work. I simply set user agent in order API to output a result with given parameters.Northwester
Ok. thats great. Thanks a million!Polycarp
My pleasure, I wish you best of luck with your projects.Northwester
L
1

Try replacing the double quotes for single quotes. It's the simplest answer listed here:

php file_get_contents and &

The top one is more involved, but is probably a safer bet.

Luanneluanni answered 8/5, 2015 at 9:33 Comment(0)
C
0

Give a try to :

$url = html_entity_decode($url);
file_get_contents($url);

For details on html_entity_decode

UPDATED

Try replacing

& with & in url as below

$url = "https://api.gdax.com/products/BTC-USD/candles?start=".date($format,$starting_date)."&end=".date($format,$ending_date)."&granularity=".$granularity;
Crystalcrystalline answered 8/5, 2015 at 9:32 Comment(0)
F
-3

You can revert the htmlspecialchars by using :

htmlspecialchars_decode();

try calling file_get_contents like so :

file_get_contents(htmlspecialchars_decode($url));

Future answered 8/5, 2015 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.