13

I'm currently creating a bootstrap website using my Fortnite stats as data and eventually posting it on my twitch feed. However, I've managed to get it to work perfectly when I run on my local machine but when I uploaded it to my hostgator directory the site loads fine it just has no data. The error I'm getting I believe refers to my JSON file but I'm assuming the only reason this error is occurring is because there is no data in there as it's not working with the Fortnite API when ran on anything other than my local machine. Has anyone else ever experienced anything like this before?

The Error:

Source map error: SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data

Source Map URL: bootstrap.min.css.map

This is my code I assume there's not much point in posting it as if it runs on my local machine logic says it's fine, right?

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.fortnitetracker.com/v1/profile/psn/myusername");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'TRN-Api-Key: mykey'
));
$response = curl_exec($ch);
curl_close($ch);
$fp = fopen("stats.json", "w");
fwrite($fp, $response);
fclose($fp);

$data = json_decode(file_get_contents("stats.json"));
$solo = $data->stats->p2;//solos data
$duos = $data->stats->p10;//duos data
$squads = $data->stats->p9;//squads data
$solo_wins = $solo->top1->valueInt;
$duos_wins = $duos->top1->valueInt;
$squads_wins = $squads->top1->valueInt;
$solo_matches = $solo->matches->valueInt;
$duos_matches = $duos->matches->valueInt;
$squads_matches = $squads->matches->valueInt;
$solo_kd = $solo->kd->valueDec;
$duos_kd = $duos->kd->valueDec;
$squads_kd = $squads->kd->valueDec;
$solo_games = $solo->matches->valueInt;
$duos_games = $duos->matches->valueInt;
$squads_games = $squads->matches->valueInt;
$solo_kills = $solo->kills->valueInt;
$duos_kills = $duos->kills->valueInt;
$squads_kills = $squads->kills->valueInt;

?>
Matt Hutch
  • 453
  • 1
  • 6
  • 20
  • Do you have a copy of the json? – Sharad Khanna Apr 18 '18 at 01:28
  • I do yes, but it exceeds the character count on a question. But I don't think the JSON is the issue as when ran on my local machine the index generates the JSON file with the data from the API. But when I run it on my HostGator directory nothing is generated the file is empty. – Matt Hutch Apr 18 '18 at 08:07
  • Can you post it on an external link because error seems to be with the Json – Sharad Khanna Apr 18 '18 at 11:02
  • You don't understand, the JSON file is empty. It's the API that generates it, as I can't connect to the API I can't generate the JSON file. However, if I run the exact some code on my local machine the file is then generated and the code runs fine. – Matt Hutch Apr 18 '18 at 12:04
  • 2
    You need to do two things, before `curl_close` add `if(curl_error($ch)) { echo 'error:' . curl_error($ch); }` also add `curl_setopt($ch, CURLOPT_VERBOSE, 1);` before `curl_exec`. You are getting an error when running on server and you are not printing it. Also add a `var_dump($response);` to print the response you get – Tarun Lalwani Apr 26 '18 at 19:04
  • Consider using https://jsonlint.com or any other service of this kind to validate contents of your `stats.json` file. – Bushikot Apr 27 '18 at 14:12
  • How is it going so far? JSON in your file is valid? – Bushikot Apr 28 '18 at 11:25
  • Have you opened `stats.json` in a plain text editor to confirm its empty? – Scuzzy May 03 '18 at 01:33
  • 1
    As mentioned by @TarunLalwani, Please print the `curl error` and check on Hostgator, situations can be that cURL is not set properly in your Hostgator account. – Mohd Belal May 03 '18 at 06:58

1 Answers1

2

You're correct that this problem has occurred to others: it seems that Hostgator does not automatically allow SSH and cURL to be combined (link). If you google "Hostgator cURL you already get some useful responses. Several people (such as this one and a commenter here one) have reported that updating to PHP 5.5/5.6 resolves the issue. I'd certainly suggest you check the suggestions made by Stanislav in the comments of the later.

Another solution might lie in disabling CURLOPT_SSL_VERIFYPEER and the other SSL options.

Without the actual error message returned by curl_exec however, your current question does not supply us with enough information to pinpoint the issue.

Martijn
  • 417
  • 2
  • 8