php - Why is this JSON refusing to get a response? And fails on a curl http://.... response.json command -


i having trouble getting json output correctly. have been struggling number of days now.

the initial error receiving was:

error domain=nscocoaerrordomain code=3840 "invalid value around character 0." userinfo={nsdebugdescription=invalid value around character 0.}

this output in browser when using request: http://www.quasisquest.uk/keepscore/gettotalsstats.php?player_id=2

{"stats":{"totalwins":10,"totaldraws":6,"totallosses":3,"winpercentage":"52.63%","goalsscored":40,"goalsconceded":30,"goaldifference":10}}

after discussion on here mentioned problem may php side @ loss since simple echo json_endcode ("test") , taking out mysql interference has not worked.

i have spoke hosting company have said fine server-side.

this swift function:

override func viewdidappear(_ animated: bool) {      //communitiestableview.reloaddata()    let isuserloggedin = userdefaults.bool(userdefaults.standard)(forkey: "isuserloggedin");      if(!isuserloggedin)     {         self.performsegue(withidentifier: "loginview", sender: self);     }        let myurl = url(string: "http://www.quasisquest.uk/keepscore/gettotalsstats.php?");     var request = urlrequest(url:myurl!);     request.addvalue("application/json",forhttpheaderfield: "content-type")     request.addvalue("application/json",forhttpheaderfield: "accept")     request.httpmethod = "post";       let poststring = "player_id=\(self.playerid)";      request.httpbody =  poststring.data(using: string.encoding.utf8);      print (myurl!,poststring)      let task = urlsession.shared.datatask(with: request) { (data: data?, response: urlresponse?, error: error?) in          dispatchqueue.main.async             {                   if error != nil {                     print("error=\(error)")                     return                 }                  do{                     let json = try jsonserialization.jsonobject(with: data!, options: .allowfragments) as? anyobject                      print (json!)                      if let arr = json?["stats"] as? [[string:anyobject]] {                         self.totalwins = arr.flatmap { $0["totalwins"]}                     print("here: ", self.totalwins)                    }                   } catch{                     print(error)                 }          }     }     task.resume()  } 

below php script:

 <?php  require ("conn.php"); require ("mysqldao.php");  $playerid = htmlentities($_request["player_id"]); $returnvalue = array();  if(empty($playerid)) { $returnvalue["status"] = "error"; $returnvalue["message"] = "missing required field"; echo json_encode($returnvalue); return; }  $dao = new mysqldao(); $dao->openconnection();  //wins losses draws , %  $playertotalwins = array(); $playertotalwins = $dao->gettotalwins($playerid);  $playertotaldraws = array(); $playertotaldraws = $dao->gettotaldraws($playerid); $playertotallosses = array(); $playertotallosses = $dao->gettotallosses($playerid);  $winpercentage = (($playertotalwins) / (($playertotalwins) + ($playertotaldraws) + ($playertotallosses)));      $newwinpercent = sprintf("%.2f%%", $winpercentage* 100);  //goals scored  $goalsscoredhome = array(); $goalsscoredhome = $dao->getgfhome($playerid); $goalsscoredaway = array(); $goalsscoredaway = $dao->getgfaway($playerid); $totalgoalsscored = ($goalsscoredhome + $goalsscoredaway);  //goals conceded  $goalsconcededhome = array(); $goalsconcededhome = $dao->getgchome($playerid); $goalsconcededaway = array(); $goalsconcededaway = $dao->getgcaway($playerid); $totalgoalsconceded = ($goalsconcededhome + $goalsconcededaway);  //goal difference  $goaldifference = ($totalgoalsscored - $totalgoalsconceded);  //print out  header('content-type: application/json');  $arr = array('totalwins' => $playertotalwins, 'totaldraws' => $playertotaldraws, 'totallosses' => $playertotallosses, 'winpercentage' => $newwinpercent, 'goalsscored'=>$totalgoalsscored, 'goalsconceded'=>$totalgoalsconceded, 'goaldifference' => $goaldifference);  echo json_encode (array('stats' => $arr));  $dao -> closeconnection();  ?> 

it has been suggested run curl http://... -> response.json seems save file 0 bytes.

i totally @ loss , new swift appreciated.

so problem in line let myurl = url(string: "http://www.quasisquest.uk/keepscore/gettotalsstats.php?"); ? kicking out.

solved :)


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -