0

I'm using file_get_contents in my script. First time this function is running, everything is fine. Second time, there is no response (as fare as I can see) and the is running slowly on my webserver. I don't have this problem running locally, only on one.com. The problem seems to have occurred recently.

<?php

$adresse_sok = "https://example.com/adresser/v1/sok?sok=kirkegata&fuzzy=false&&utkoordsys=25833&treffPerSide=1&side=0&asciiKompatibel=true" ;

$adresse_response = file_get_contents($adresse_sok);
$adresse_data = json_decode($adresse_response,true);
$test = $adresse_data['adresser'][0];
$adressetekst = $adresse_data['adresser'][0]['adressetekst'];
$poststed = $adresse_data['adresser'][0]['poststed'];
$lat = round($adresse_data['adresser'][0]['representasjonspunkt']['lat']);
$lon = round($adresse_data['adresser'][0]['representasjonspunkt']['lon']);

$ost = $lat;
$nord = $lon;

$url = "https://gts.nve.no/api/GridTimeSeries/" . $lon . "/". $lat ."/2022-12-24/2022-12-24/sd.json";

// DOES NOT WORK
$response = file_get_contents($url);
$data = json_decode($response,true);

$snodybde = $data['Data'][0];

?>

When adding var_dump($http_response_header); this is returned:

array(10) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(13) "Server: nginx" [2]=> string(35) "Date: Thu, 24 Nov 2022 10:45:40 GMT" [3]=> string(30) "Content-Type: application/json" [4]=> string(19) "Content-Length: 781" [5]=> string(17) "Connection: close" [6]=> string(30) "Access-Control-Allow-Origin: *" [7]=> string(48) "Access-Control-Allow-Methods: GET, POST, OPTIONS" [8]=> string(112) "Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range" [9]=> string(59) "Access-Control-Expose-Headers: Content-Length,Content-Range" }

When doing this from a different server which is returning data, var_dump($http_response_header); returns:

array(10) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(22) "Cache-Control: private" [2]=> string(45) "Content-Type: application/json; charset=utf-8" [3]=> string(26) "Server: Microsoft-IIS/10.0" [4]=> string(24) "X-AspNetMvc-Version: 5.2" [5]=> string(27) "X-AspNet-Version: 4.0.30319" [6]=> string(21) "X-Powered-By: ASP.NET" [7]=> string(35) "Date: Thu, 24 Nov 2022 10:42:49 GMT" [8]=> string(17) "Connection: close" [9]=> string(19) "Content-Length: 240" }
Johs
  • 73
  • 1
  • 7
  • You are doing a webrequest. Maybe the endpoint is limiting you. – Markus Zeller Nov 23 '22 at 21:10
  • Ok. Do you know why this works when running locally on wamp and not on my webserver? @MarkusZeller This is an example url: https://gts.nve.no/api/GridTimeSeries/317548/7074197/2022-12-24/2022-12-24/sd.json – Johs Nov 23 '22 at 21:18
  • We'll need more info than "does not work". What response do you get from the remote server to your file_get_contents request, if any? Have you examined any response headers as well as the content? Have you checked there isn't a firewall rule blocking the connection, or anything like that? We can't tell you what's wrong because you've provided no technical information about what happens to the request. – ADyson Nov 23 '22 at 21:36
  • Thanks! I don’t get any response. I have not checked for firewall rules and do not know hos to do so. – Johs Nov 23 '22 at 21:40
  • What is the output of `var_dump($http_response_header);` when you add it after the second `file_get_contents` – Moshe Gross Nov 24 '22 at 02:11
  • array(10) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(13) "Server: nginx" [2]=> string(35) "Date: Thu, 24 Nov 2022 06:07:22 GMT" [3]=> string(30) "Content-Type: application/json" [4]=> string(19) "Content-Length: 781" [5]=> string(17) "Connection: close" [6]=> string(30) "Access-Control-Allow-Origin: *" [7]=> string(48) "Access-Control-Allow-Methods: GET, POST, OPTIONS" [8]=> string(112) "Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range" [9]=> string(59) "Access-Control-Expose-Headers: Content-Length,Content-Range" } – Johs Nov 24 '22 at 06:08
  • So are you actually not getting any response (then where would those headers you just quoted come from?) - or are you "just" getting a null value from the `json_decode` call perhaps? What did `$response` actually contain? – CBroe Nov 24 '22 at 07:20
  • I does not got a null value. It should contain an array with some integer data. The array is supposed to look like this: Array ( [Theme] => sd [FullName] => Snødybde v2.0.1 [NoDataValue] => 65535 [X] => 317548 [Y] => 7074197 [StartDate] => 24.12.2022 06:00:00 [EndDate] => 24.12.2022 06:00:00 [PrognoseStartDate] => [Unit] => cm [TimeResolution] => 1440 [Altitude] => 4 [Data] => Array ( [0] => 65535 ) ) – Johs Nov 24 '22 at 07:40
  • We asked what it actually contains, not what its supposed to. What does `var_dump($response);` show you? (when you add it after the second file_get_contents)? – ADyson Nov 24 '22 at 07:59
  • Ah, i see. That is the array above. The array was to long to write anything more, but this is the start as far as there are characters left in this comment: array(10) { [0]=> string(15) "HTTP/1.1 200 OK" [1]=> string(13) "Server: nginx" [2]=> string(35) "Date: Thu, 24 Nov 2022 06:07:22 GMT" [3]=> string(30) "Content-Type: application/json" [4]=> string(19) "Content-Length: 781" [5]=> string(17) "Connection: close" [6]=> string(30) "Access-Control-Allow-Origin: *" [7]=> string(48) "Access-Control-Allow-Methods: GET, POST, OPTIONS" [8]=> string(112) "Access-Control-Allow-Headers: DNT,User-Agent – Johs Nov 24 '22 at 08:13
  • This is pretty weird. It seems to be returning the HTTP headers in the response instead of (or posssibly as well as?) the body content. I [couldn't find much](https://www.google.com/search?q=file_get_contents+returns+headers) about this except [this question](https://stackoverflow.com/questions/36769982/file-get-contents-returns-http-headers-in-content) which suggests an odd solution, but it's worth trying. – ADyson Nov 24 '22 at 10:17
  • P.S. Have you also verified that `$url` contains sensible values when you're running this on the server? It occurs to me it could also be an issue with the earlier request, resulting in the later one not going to the correct URL. So doing `var_dump($url);` would also be sensible. – ADyson Nov 24 '22 at 10:20
  • `var_dump($url]` looks as it should, i think. Returning: string(82) "https://gts.nve.no/api/GridTimeSeries/317548/7074197/2022-12-24/2022-12-24/sd.json" – Johs Nov 24 '22 at 10:24
  • I tried the script on another server, and it returned this perfectly. Can it be that my responses is being blocked from the API, as I do a bunch of queries each day? – Johs Nov 24 '22 at 10:26
  • It's certainly possible, yes. Although the fact you're getting a 200 OK response (according to those headers) is confusing, in that case. You could check with the API provider whether they have any policy about number of requests or anything. But please also try the fix suggested in the link I gave above. – ADyson Nov 24 '22 at 10:31
  • P.S. When you get that `array(10) { [0]=> string(15) "HTTP/1.1 200 OK"...` response, I know you couldn't paste it all here (although can [edit] your question rather than putting it in comments, if you have a bigger update), but at the end of the string of header data, is there also some other content at the end, more like what you'd expect, or containing an error message or anything? Paste the whole lot in your question if you're unsure. – ADyson Nov 24 '22 at 10:38
  • Theres no data at the end of the header, more like what I expected. I added the full response to my initial question. I also added a header response I got from the other server, working just fine. I have contacted the API provider, but I will look in to the questions mentioned earlier as well. – Johs Nov 24 '22 at 10:50

0 Answers0