-1

Some code that has been working for months is suddenly not working. I have narrowed it down to the PHP file_get_contents portion. Here is a snip of the code:

// This upper portion was added to send a user-agent with the request
$opts = [
    "http" => [
        "method" => "GET",
        "header" => "Accept-language: en\r\n" .
        "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",
    ],
];
$context = stream_context_create($opts);
// End of the added part except for the actual get-file-contents request line


$ViewLat = 40.7;
$ViewLng = -73.9;
$Distance = 1000;
$input1 = "https://api.helium.io/v1/hotspots/location/distance?lat=40.7&lon=-73.9&distance=1000";
$input2 = "https://api.helium.io/v1/hotspots/location/distance/?lat=$ViewLat&lon=$ViewLng&distance=$Distance";
$json_string = file_get_contents(urlencode($input2), false, $context); // added the last 2 arguments

var_dump($ViewLat);
echo "<br>";
var_dump($ViewLng);
echo "<br>";
var_dump($Distance);
echo "<br>";
var_dump($input1);
echo "<br>";
var_dump($json_string);

It doesn't matter if I use $input1 or $input2, they both return bool(false) in the var_dump ouput. But if you put the string directly into a browser, it returns an array of data.

This code was working back in August/September but now does not.

Here is the var_dump ouput:

float(40.7)
float(-73.9)
int(1000)
string(84) "https://api.helium.io/v1/hotspots/location/distance?lat=40.7&lon=-73.9&distance=1000"
bool(false)
  • 1
    The may have detected that this is a automated request and blocked it. Did you try sending the request in postman? – MaartenDev Nov 23 '21 at 19:14
  • Have a read of https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https and see if anything helps. – Nigel Ren Nov 23 '21 at 19:15
  • urlencode is making a mess of your URL. `var_dump(urlencode($input2))` and you'll see exactly what I mean. – aynber Nov 23 '21 at 19:19
  • However, if you don't encode it, it still doesn't work with `file_get_contents`. I tried it and got `failed to open stream: HTTP request failed! HTTP/1.1 429 Slow down gun powder!`, which means it's being blocked by the host for too many requests. – aynber Nov 23 '21 at 19:20
  • I just tried some of the suggestions from question 1975461 but nothing worked. I am looking through my php.ini file to see what settings are there but I use this command to access other APIs without any issues. I am not sure how to send a header that emulates a browser request. – David Headrick Nov 23 '21 at 20:14
  • As for var_dumping urlencoded strings, that is exactly what it is supposed to do. Those are the encoded characters. – David Headrick Nov 23 '21 at 20:17
  • I just implemented it in the actual production code and it is working fine without a urlencode. – David Headrick Nov 23 '21 at 21:12

1 Answers1

0

Success!!!! I had to add some user-agent info to the file-get-contents request. I will update the code above to add what worked.