Saturday, 15 September 2012

php - post data to api using json -


The code I wrote to send data to an API using the POST method in JSON but answer me invalid Anybody can tell me what is wrong with the code

  $ post = array ("operatorCode" => "9", "scheduleCode" => "84W92XC8LOBAF3KZP4", " = "Gtc: mediawiki-xid =" gtc: mediawiki-xid = "gtc: mediawiki-xid =" 191,933 "" droppingPointCode "= & gt; = "Gtc: ("Seat number" = & gt; "40", "name" = & gt; "** ***", "age" =>, "20", " Ing "= & gt;" I ")); $ Data_string = json_encode ($ post); Echo $ data_string; $ Ch = url_init ('http: // API user name = *****?'); Curl_setopt ($ CH, CURLOPT_CUSTOMREQUEST, "POST"); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, array ('data' = & gt; $ data_string)); Curl_setopt ($ CH, CURLOPT_RETURNTRANSFER, true); $ Result = curl_xac ($ CH); $ Result = json_decode ($ result); Var_dump ($ result); Echo '& lt; Pre & gt; '; Print_r ($ result);  

Response API

  stdClass Object ([code] = & gt; 0 [errorCode] = & gt; ER30 [errorDesc] = & gt; Invalid data [message] = & gt; failure)  

Sample URL:

  from http: // & lt; Server name & gt; /bookbustickets/rest/json/2.0/tempbooking? User name = **** and password = ******  

Sample input data:

  data = {"operatorCode": "2586 "", "Schedule.code": "7Q52586C3YG8KJ350F2F5V264", "travelDa to": "2015-03-10", "fromStationCode": "76", "toStationCode": "75", "boardingPointCode": "284,936", "droppingPointCode" "0000", "email": "Test@gmailkcom", "mobile": "+ 9 876 543 210", "Traveler": [{ "seatNumber": "3", "No 9 I": "Test" "Age": 20, "Gender": "Male"}, {"seatNumber": "4", "Name": "Test", "Age": 20, "G Anderson": "Male"}}} < / code> 

Well the problem is that the "Traveler" object Series of Try to change your code for

should be
  $ passenger1 = new stud class (); $ Passenger 1- & gt; Seat number = "40"; $ Passenger 1- & gt; Name = "*****"; $ Passenger1- & gt; Age = "20"; $ Passenger 1- & gt; Gender = "male"; $ Passenger2 = new stud class (); $ Passenger 2- & gt; Seat number = "41"; $ Passenger2- & gt; Name = "*****"; $ Passenger2- & gt; Age = "21"; $ Passenger 2- & gt; Gender = "female"; $ Post = array ("operatorCode" = & gt; "9", "scheduleCode" => "84W92XC8LOBAF3KZP4", "travelDate" => "2015-03-20", "fromStationCode" => 84 "," toStationCode "= & gt;" 76 "," boardingPointCode "= & gt;" 191,933 "" droppingPointCode "= & gt;" 0000 "," email "= & gt;" ******* ** "," Mobile "= & gt;" ******* "," Traveler "=> Array ($ passenger 1, $ passenger 2));  

This will result in the following

  {"operatorCode": "9", "scheduleCode": "84W92XC8LOBAF3KZP4", "travelDate": "2015 - 03-20 "," fromStationCode ":" 84 "," toStationCode ":" 76 "," boardingPointCode ":" 191,933 "" droppingPointCode ":" 0000 "," Email ":" ******* ** "," "Mobile": "*******", "traveler": [{"seat number": "40", "name": "*****", "age": "20" , "Gender": "male"}, {"seatNumber": "41", "name": "*****", "age": "21", "gender": "woman"}}} < / code> 

In addition, I note that you are sure to use why you

  curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "post");  

is not

  curl_setopt ($ ch, CURLOPT_POST, true);  

Be a better choice?


No comments:

Post a Comment