PHP - Decode Json And Add It To Mysql
hi all
i have some json that i want to decode and add to mysql could you give me some help? i need to add each of these as a row here is the json Code: [Select] [ { "DayNumber": 1, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 1, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 1, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 1, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 1, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 2, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 2, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 2, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 2, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 2, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 2, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10P9D6", "ClassDescription": "Physical Development", "StaffID": 83, "Room": "G3" }, { "DayNumber": 3, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:30:00 AM", "DefinitionTimeTo": "9:25:00 AM", "ClassCode": "10P9D6", "ClassDescription": "Physical Development", "StaffID": 1455, "Room": "G3" }, { "DayNumber": 3, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:25:00 AM", "DefinitionTimeTo": "10:15:00 AM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 3, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:35:00 AM", "DefinitionTimeTo": "11:30:00 AM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 3, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:30:00 AM", "DefinitionTimeTo": "12:15:00 PM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 3, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10C9H1", "ClassDescription": "Chapel", "StaffID": 1071, "Room": "C1" }, { "DayNumber": 3, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "A3" }, { "DayNumber": 4, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 4, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 4, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 4, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 4, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 5, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 5, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 5, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 5, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 5, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 6, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 6, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 6, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10C9H1", "ClassDescription": "Chapel", "StaffID": 1071, "Room": "C1" }, { "DayNumber": 6, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 6, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 6, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 7, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 7, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10CWA1", "ClassDescription": "Web Authoring", "StaffID": 607388, "Room": "CY34" }, { "DayNumber": 7, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10P9D6", "ClassDescription": "Physical Development", "StaffID": 1455, "Room": "G3" }, { "DayNumber": 7, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 8, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:30:00 AM", "DefinitionTimeTo": "9:25:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "S21" }, { "DayNumber": 8, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:25:00 AM", "DefinitionTimeTo": "10:15:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "S21" }, { "DayNumber": 8, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:35:00 AM", "DefinitionTimeTo": "11:30:00 AM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 8, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:30:00 AM", "DefinitionTimeTo": "12:15:00 PM", "ClassCode": "10APX4", "ClassDescription": "Photography", "StaffID": 620143, "Room": "CY14" }, { "DayNumber": 8, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 8, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 9, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10P9D6", "ClassDescription": "Physical Development", "StaffID": 83, "Room": "G3" }, { "DayNumber": 9, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" }, { "DayNumber": 9, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 9, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10CBP1", "ClassDescription": "Programming ", "StaffID": 607388, "Room": "CY17" }, { "DayNumber": 9, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 9, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "S23" }, { "DayNumber": 10, "PeriodNumber": 1, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 1, "DefinitionTimeFrom": "8:50:00 AM", "DefinitionTimeTo": "9:45:00 AM", "ClassCode": "10GHZ6", "ClassDescription": "Hazard Geography", "StaffID": 608055, "Room": "S22" }, { "DayNumber": 10, "PeriodNumber": 2, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 2, "DefinitionTimeFrom": "9:45:00 AM", "DefinitionTimeTo": "10:35:00 AM", "ClassCode": "10F9M1", "ClassDescription": "Form Period", "StaffID": 615792, "Room": "T11" }, { "DayNumber": 10, "PeriodNumber": 3, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 3, "DefinitionTimeFrom": "10:55:00 AM", "DefinitionTimeTo": "11:50:00 AM", "ClassCode": "10M9B5", "ClassDescription": "Mainstream Mathematics 2", "StaffID": 625461, "Room": "A4" }, { "DayNumber": 10, "PeriodNumber": 4, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 4, "DefinitionTimeFrom": "11:50:00 AM", "DefinitionTimeTo": "12:40:00 PM", "ClassCode": "10AMS2", "ClassDescription": "Media Studies", "StaffID": 622915, "Room": "CY14" }, { "DayNumber": 10, "PeriodNumber": 5, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 5, "DefinitionTimeFrom": "1:30:00 PM", "DefinitionTimeTo": "2:25:00 PM", "ClassCode": "10QCS5", "ClassDescription": "Literacy - Curriculum Support2", "StaffID": 601807, "Room": "A1" }, { "DayNumber": 10, "PeriodNumber": 6, "PeriodNumberSeq": 1, "DefinitionPeriodNumber": 6, "DefinitionTimeFrom": "2:25:00 PM", "DefinitionTimeTo": "3:15:00 PM", "ClassCode": "10EWB2", "ClassDescription": "Writing Workshop 1", "StaffID": 619597, "Room": "T22" } ] thanks Matt Similar TutorialsI am using multiple levels of JSON data coming into php from a C# application, as in:
return new RootObject() { ID_Project = 4, Name_Project = "Test", Receiver_ID = 4, Receiver_Name = "ABCDE", UserID = 20, UserRole = new User_Role() { ID_User = 20, Role_User = "level 3", User_Role_Description = "U", UserGroup = new List<user_group>() { new User_Group() { ID_UserGroup = 30, Name_UserGroup = "usergroup8", UserID = 20 }, new User_Group() { ID_UserGroup = 31, Name_UserGroup = "usergroup9", UserID = 21 }, new User_Group() { ID_UserGroup = 32, Name_UserGroup = "usergroup10", UserID = 22 } } } }; i am having troubles accessing the second level: UserRole and 3rd level: User_Group in my php script. im currently trying like this: foreach ($phpArray as $key => $value) { echo "<h2>$key</h2>"; foreach ($value as $k => $v) { echo "$k | $v "; foreach ($v as $key1) { echo "$key1 "; } } } does anyone have an idea? Thanks, Revathy Hey guys i got an api and it returns :
[ $json = file_get_contents(URL); $data = json_decode($json,true); $Decoded = $data['id'][0]; HERE no mater what i put i get and error Undefined index: id in /home/handshak/public_html/XC/get2.php on line 9 echo "<pre>"; print_r($Decoded); exit; What type of json structure is that? EDIT: if i use:
$contents = file_get_contents($json); i get:
[14-Apr-2019 15:38:32 UTC] PHP Warning: file_get_contents([{"id":"1","package_name":"1 Month Full + Adult - 2 Devices","is_trial":"0","is_official":"1","trial_credits":"0","official_credits":"1","trial_duration":"24","trial_duration_in":"hours","official_duration":"1","official_duration_in":"months","groups":"[4,5,6,8]","bouquets":"[27]","can_gen_mag":"1","only_mag":"0","output_formats":"[1,2,3]","is_isplock":"0","max_connections":"2","is_restreamer":"0","force_server_id":"0","can_gen_e2":"1","only_e2":"0","forced_country":"","lock_device":"0"},{"id":"2","package_name" in /home/handshak/public_html/XC/get2.php on line 7 Hello everyone, I'm new here. Amazing forum, I found some help in the past by searching google.
Now I am looking for some help with the following code, I use this code to grab a JSON Feed and list the items in my page, this code works fine to get all the items, but I need to limit the number of items, otherwise It will load more than 500 items in my page and never stop loading.
Is there a way to limit the number of items grabbed from the JSON Feed?
Let's say, list 50 items in page 1, another 50 in page 2 and so on?
<?php $data = file_get_contents('http://json-url.com/type=json'); $decode = json_decode($data, true); foreach($decode as $d) { if($d['status'] == true){ echo '<div class="div">'; echo '<a href="'.$d['item_url'].'">'; echo '<img src="'.$d['item_image']['jpeg_thumbnail'].'" />'; echo '<span>'.$d['item_name'].'</span>'; echo '</a>'; echo '</div>'; } } ?>Thanks! I am having a nightmare sending / recieving some JSON..... I have a test page which creates a JSON packet and posts it to another page. This page then processes it and returns a response... the data is in the post, but after decoding it the data seems to be lost.... Here is the code for the sending page: Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <?php require_once ("inc/jsonwrapper/JSON/JSON.php"); $RequestID ='121'; // :ID to which this request relates to allow site to keep track of things $Method = 'login'; //:Which method the call is directed at $referer = 'abc'; // :Name of referring network $affiliateid =2468; // :ID of referring site in affiliate network if any $user_Country ='gb'; // :Users two letter country code $UserID = '12345'; // :Numeric ID to identify players in game database belonging to network or portal $GameuserID = '1' ; // UserID in game database $params = array ( 'referer' => $referer , 'affiliateid' => $affiliateid , 'userCountry' => $user_Country , 'UserID' => $UserID , 'GameuserID' => $GameuserID); $response = array ( 'jsonrpc'=>'2.0', 'id'=> $RequestID , 'Method' => $Method , 'params' => $params) ; $data = json_encode($response); // URL of web api page $url = 'http://xxx.com/webapi.php'; $reply = do_post_request($url, $data); // setup new JSON service $json = new Services_JSON(); $jsonPacket = $json->decode($reply,true); //THIS IS WHERE I PRINT THE RESPONSE echo print_r ($jsonPacket,1); function do_post_request($url, $data, $optional_headers = null) { $params = array('http' => array( 'method' => 'POST', 'header'=>"Content-Type: text/xml; charset=utf-8", 'content' => $data )); if ($optional_headers !== null) { $params['http']['header'] = $optional_headers; } $ctx = stream_context_create($params); $fp = @fopen($url, 'rb', false, $ctx); if (!$fp) { throw new Exception("Problem with $url, $php_errormsg"); } $response = @stream_get_contents($fp); if ($response === false) { throw new Exception("Problem reading data from $url, $php_errormsg"); } return $response; } ?> And the replying page Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <? //setup database connection require_once('globals.php'); //setup JSON require_once ("inc/jsonwrapper/JSON/JSON.php"); $secret_Key = '12345';//SECRET KEY $app_Id = '6789';//APP ID $token = ''; // setup new JSON service $json = new Services_JSON(); // accept incoming POST data $input = $GLOBALS['HTTP_RAW_POST_DATA']; //hashes $hash = $_GET['authHash']; $sHash = strtolower(md5($input . $secret_Key)); $hash = 0; $sHash = 0; if (strcmp($sHash, $hash) == 0) { //check if incoming is JSON as we MIGHT use XML later too $isJSON= substr_count($input,"json"); if ($isJSON > 0){ processJSON ($input); } global $hash, $sHash; } //JSON FUNCTION TO PARSE JSON function processJSON($input){ global $json; $jsonPacket = $json->decode($input,true); $json_RequestId = $jsonPacket->id; //check method and go to correct function switch ($jsonPacket->Method){ case 'Game.login': game_login ($jsonPacket, $json_RequestId) ; break; } } //JSON FUNCTIONS function game_login($jsonPacket, $json_RequestId){ $aid = ($jsonPacket->params->affiliateid); $country_code = ($jsonPacket->params->userCountry); $GameuserID = ($jsonPacket->params->GameuserID); $UserID = ($jsonPacket->params->UserID); //mail ("martyn@staggan.com" , "SNSUserID" , $UserID ); $result_userIDcheck = mysql_query("SELECT snsUserid FROM player where player_id = $GameuserID;"); $result_UserID = mysql_result ($result_userIDcheck, 0); if ($UserID == $result_UserID){ $result = 'OK'; $id = $json_RequestId; //create token global $secret_Key ; $token = hash ('md5', $secret_Key.$aid.$GameuserID); $logindetails = array ( 'token=' => $token , 'UID'=> $GameuserID , 'snsUID' => $UserID , 'aid' => $aid , 'name' => $name); $params= array ('result'=>$result, 'logindetails'=> $logindetails, 'GameuserID'=>$GameuserID); $response = array ( 'jsonrpc'=>'2.0', 'id'=> $id , 'result' => $params) ; echo json_encode($response); } else mail ("test@test.com", "login", 'false'); } ?> If I print the $reply var I see this: {"jsonrpc":"2.0","id":"121","result":{"result":"OK","logindetails":{"token=":"68c49918c353a3e5d86d165da1cb2f72","UID":"1","snsUID":"12345","aid":"6789","name":null},"GameuserID":"1"}} But trying to decode to an array leaves me with nothing.... Any help would be greatly appreciated Hi all,
I'm posting JSON to an API call. If I use a var_dump within the API it will return correctly, although if I try to var_dump within the function it will return NULL.
API:
$json = file_get_contents('php://input'); if(isset($_GET['form_id']) && intval($_GET['form_id'])>0) { if(isset($json)) { $records = $questionnaire->logFormAnswers($_POST['answers'],$_GET['form_id']);Then using the logFormAnswers function... Function: public function logFormAnswers($answers = "", $form_id = 0) { $answers = json_decode($json,true); var_dump($answers);The JSON: [{"question_id":"3","answer":"dsf"},{"question_id":"3","answer":"sdfsdfdsfsd"}]Eventually, once the JSON has been decoded, it will be inserted into a database within the function. This topic has been moved to PHP Freelancing. http://www.phpfreaks.com/forums/index.php?topic=323713.0 help required for noobie(no hair left) all i want to be able to do is use the individual elements of the array in javascript eg companyname or companyimage but just getting my head in my hand. mysql_select_db("herefordinfo") or die(mysql_error()); echo "Connected to Database"; $dbQuery = 'SELECT * FROM companies'; $dbResult = mysql_query($dbQuery) or die ("Could not read data because " . mysql_error()); $RowCount = mysql_numrows($dbResult); while ($pubs = mysql_fetch_assoc($dbResult)) { $array[] = "{ {$pubs['id']}, {$pubs['companyname']}, {$pubs['companyinfo']}, {$pubs['companyimage']} }"; } echo 'var pub = [' . implode(', ', $array) . '];'; // this ?> <html> <head> <script type="text/javascript"> var jsArray = <?php echo json_encode($array); ?>; document.write(jsArray); //this </script> </head> <boby></body> </html> both php and javescritp output the results below { 1, lichfield vaults, 10 church street hereford , lich.jpg },{ 2, Black Lion, bridge street, blacklion.jpg },{ 3, Saracens Head, st martins street, saracens.jpg } Hi, I have a problem with some code not working. I need it to get the data from a mysql database and then export it as a json array in the format of id , title, author, date, imageUrl, text. (Please not that these are all variables ) PHP CODE: Code: [Select] <?php $link = mysql_connect('localhost', 'root', ''); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; $arr = array(); $rs = ("SELECT `id`, `title`, `author`, `date`, `imageUrl`, `text` FROM `items`"); while($obj = mysql_query($rs)) { $arr[0] = $obj->id; $arr[1] = $obj->title; $arr[2] = $obj->author; $arr[3] = $obj->date; $arr[4] = $obj->imageUrl; $arr[5] = $obj->text; } echo '{"items":'.json_encode($arr).'}'; ?> Thanks For Your Help borden0108 I have a simple ajax that passes data to a php script using jquery serialize. This is working good but where I am getting stuck is trying to pass json array into mysql. The json is outputting the correct data, but when I insert into db, it just says Array in the field. How can I convert(if that is the right term) json array to use in mysql. In the code I have posted, $box is an array from jquery which I normally use foreach to loop through array. What I need to do is convert $box after is has been encoded. Thanks Code: [Select] $box = $_POST['BRVbrtrv_boxnumber']; $list = array("activity" => $activity, "mobile" => $mobile, "company" => $company, "authorised" => $authorised, "service" => $service, "department" => $department, "address" => $address, "boxcount" => $boxcount, "box" => $box); $c = json_encode($list); echo $c; Hi, I have the following code that takes a list from a mySQL db and puts it into an array that shows 'gig date, venue, city' per entry: - Code: [Select] while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $array[] = ($row['date'].', '.$row['gl_venue'].', '.$row['gl_city']); } $output = json_encode($array); this outputs JSON like so: Code: [Select] ["06-05-2011, O'neills, Southend","07-05-2011, Power League, Peterborough","14-05-2011, Queen Victoria hall, Oundle",.......] how do I add keys into the array so I get something like: Code: [Select] {"gigs":[ { "gig": "date, venue, city",} ]} at least I think so anyway!?! What I want to do is eventually be able to parse a JSON listing into Objective-C and be able to call key/pairs into various different parts of an app. I'm pretty new to this so any advice would be great. Hello - I am connecting to MySql and running a query. If I use a foreach loop, I can iterate over the results and have them print to screen. However, when I try the below everything is null!
How can I add the results of my MySql query to a php array?
<?php $con=mysqli_connect("site", "user", "psasswr=ord", "db"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="SELECT * FROM test LIMIT 10"; $result = mysqli_query($con,$sql); echo json_encode($result); mysqli_free_result($result); mysqli_close($con); ?>
but what this prints is: {"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null} Hi
I have another problem with my code
I have records in my database and i want to put them in Jgrid but no records are show.
This is my php
<?php error_reporting(0); require_once 'database_connection.php'; $page = $_GET['page']; // get the requested page $limit = $_GET['rows']; // get how many rows we want to have into the grid $sidx = $_GET['sidx']; // get index row - i.e. user click to sort $sord = $_GET['sord']; // get the direction if(!$sidx) $sidx =1; // connect to the database $result = mysql_query("SELECT COUNT(*) AS count FROM utilizador"); $row = mysql_fetch_array($result,MYSQL_ASSOC); $count = $row['count']; if( $count >0 ) { $total_pages = ceil($count/$limit); } else { $total_pages = 0; } if ($page > $total_pages) $page=$total_pages; $start = $limit*$page - $limit; // do not put $limit*($page - 1) $SQL = "SELECT * FROM utilizador ORDER BY $sidx $sord LIMIT $start , $limit"; $result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error()); $responce->page = $page; $responce->total = $total_pages; $responce->records = $count; $i=0; while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $responce->rows[$i]['id']=$row[idutilizador]; $responce->rows[$i]['cell']=array($row[idutilizador],$row[nome],$row[utilizador],$row[telefone],$row[email]); $i++; } echo json_encode($responce); ?>And this is the Jgrid <html> <head> <title>jQGrid example</title> <!-- Load CSS--><br /> <link rel="stylesheet" href="css/ui.jqgrid.css" type="text/css" media="all" /> <!-- For this theme, download your own from link above, and place it at css folder --> <link rel="stylesheet" href="css/jquery-ui-1.9.2.custom.css" type="text/css" media="all" /> <!-- Load Javascript --> <script src="js/jquery_1.5.2.js" type="text/javascript"></script> <script src="js/jquery-ui-1.8.1.custom.min.js" type="text/javascript"></script> <script src="js/i18n/grid.locale-pt.js" type="text/javascript"></script> <script src="js/jquery.jqGrid.min.js" type="text/javascript"></script> </head> <body> <table id="datagrid"></table> <div id="navGrid"></div> <p><script language="javascript"> jQuery("#datagrid").jqGrid({ url:'example.php', datatype: "json", colNames:['Idutilizador','Nome', 'Utilizador', 'Telefone','Email'], colModel:[ {name:'idutilizador',index:'idutilizador', width:55,editable:false,editoptions:{readonly:true,size:10}}, {name:'nome',index:'nome', width:80,editable:true,editoptions:{size:10}}, {name:'idutilizador',index:'idutilizador', width:90,editable:true,editoptions:{size:25}}, {name:'telefone',index:'telefone', width:60, align:"right",editable:true,editoptions:{size:10}}, {name:'email',index:'email', width:60, align:"right",editable:true,editoptions:{size:10}} ], rowNum:10, rowList:[10,15,20,25,30,35,40], pager: '#navGrid', sortname: 'idutilizador', sortorder: "asc", height: 500, width:900, viewrecords: true, caption:"Atividades Registadas" }); jQuery("#datagrid").jqGrid('navGrid','#navGrid',{edit:true,add:true,del:true}); </script> </body> </html>Anything wrong with the code? Regards Hi, This is an odd one (to me anyway). I've created an API for deploying gig information in either XML or JSON format. It works fine as it is, both formats of output giving me what I want. When I take out the mySQL login information and replace it with "include 'connect_include.php';" (where the file connect_include.php contains the exact login code that I replced). The JSON output works fine but the XML output doesn't run. What could be causing this?? JSON API: -http://www.api.the-guards.co.uk/jsontest.php?apikey=123456&format=json XML API: - http://www.api.the-guards.co.uk/jsontest.php?apikey=123456&format=xml API code: Code: [Select] <?php if (($_GET['apikey'])==123456 and isset($_GET['format'])){ //Set variables $format = strtolower($_GET['format']); //Connect to the Database include 'connect_include.php'; //Run our query $result = mysql_query(' SELECT * FROM tg_gig_list where gl_date >= curdate() and gl_publish = 1 order by gl_date '); //Preapre our output if($format == 'json') { while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $array[] = array($row['gl_date']=>($row['gl_venue'].', '.$row['gl_city'])); }; $output = json_encode($array); //Output the output. echo $output; header('Content-type: application/json'); } elseif($format == 'xml') { header('Content-type: text/xml'); $output = "<?xml version=\"1.0\"?>\n"; $output .= "<tggiglist>\n"; for($i = 0 ; $i < mysql_num_rows($result) ; $i++){ $row = mysql_fetch_assoc($result); $output .= "<giglist> \n"; $output .= "<giglist_date>" . $row['gl_date'] . "</giglist_date> \n"; $output .= "<giglist_venue>" . $row['gl_venue'] . "</giglist_venue> \n"; $output .= "<giglist_city>" . $row['gl_city'] . "</giglist_city> \n"; $output .= "</giglist> \n"; } $output .= "</tggiglist>"; } } else { die ('no way'); } //output the output echo $output; ?> Contents of 'connect_include.php: Code: [Select] <?php mysql_connect("database.lcn.com", "dbname", "password") or die(mysql_error()); mysql_select_db("db") or die(mysql_error()); ?> .....this is the same code that I replaced. Obviously, the 'connect' works ok as the JSON version runs fine. There's something being added to the scripting when I use the include file that isn't there when the code goes straight in.....I think????
An affiliate marketer refers some products which are stored in a simple array. $all_items_sold = '{"7777":{"item":"hammer","price":"4.99"},"8888":{"item":"nail","price":"1.99"},"9999":{"item":"apple","price":"2.00"}}'; $referred_by_Affiliate = array('1234','8888','7777');
So, out of all the 3 items that sold, only 2 of them were referred by the affiliate marketer.
Currently I do this:
Is there a "best practices" way to accomplish this?
Thank you. How to decode this? Code: [Select] http://www.mediafire.com/?niwske5oy33wzrz Hi all, Firstly, i apologise for the long post, but I wanted to give you an overall feel for what I am trying to achieve. The question is at the end!! My Plan is to take emails and stores these in MySql for late retrieval and display. The email could be plain text or html and may contain any number of file attachments (or none)! I have no problem getting the email or saving it into SQL, but my problem is decoding it and extracting any attachments. Ultimately in the SQL database I will have all the usual parts (To, From, etc) plus the body of the email (plain and/or html) and a list of attachments. Everything I have read so far involves getting the emails from an iMap mail server however my emails are all contained in individual text files. My question is really to enquiry what classes/scripts are available to help me decode the text file (email) and gather all the information I need. Any help or pointers much appreciated Thanks David I have implemented the base64_encode() function with some custom mapping table so I could encode some binary data more securely without anyone knowing how to decode it here is the class <?php class Base64 { /** * I have changed letter placement (P <=> x, S <=> 9) and the cases * You can completely redo the mapping table */ private static $BinaryMap = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', // 7 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'x', // 15 'q', 'r', '9', 't', 'u', 'v', 'w', 'x', // 23 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', // 31 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', // 39 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', // 47 'W', 'P', 'Y', 'Z', '0', '1', '2', '3', // 55 '4', '5', '6', '7', '8', 'S', '+', '/', // 63 '=', // padding char ); public function __construct() {} public function base64_encode($input) { $output = ""; $chr1 = $chr2 = $chr3 = $enc1 = $enc2 = $enc3 = $enc4 = null; $i = 0; // $input = self::utf8_encode($input); while($i < strlen($input)) { $chr1 = ord($input[$i++]); $chr2 = ord($input[$i++]); $chr3 = ord($input[$i++]); $enc1 = $chr1 >> 2; $enc2 = (($chr1 & 3) << 4) | ($chr2 >> 4); $enc3 = (($chr2 & 15) << 2) | ($chr3 >> 6); $enc4 = $chr3 & 63; if (is_nan($chr2)) { $enc3 = $enc4 = 64; } else if (is_nan($chr3)) { $enc4 = 64; } $output .= self::$BinaryMap[$enc1] . self::$BinaryMap[$enc2] . self::$BinaryMap[$enc3] . self::$BinaryMap[$enc4]; } return $output; } public function utf8_encode($input) { $utftext = null; for ($n = 0; $n < strlen($input); $n++) { $c = ord($input[$n]); if ($c < 128) { $utftext .= chr($c); } else if (($c > 128) && ($c < 2048)) { $utftext .= chr(($c >> 6) | 192); $utftext .= chr(($c & 63) | 128); } else { $utftext .= chr(($c >> 12) | 224); $utftext .= chr((($c & 6) & 63) | 128); $utftext .= chr(($c & 63) | 128); } } return $utftext; } } ?> and the usage as follows: <?php $string = pack('H*', "31c85c5aaa56c1f0102301ea497d0ab010e4e131af261787"); //HEX echo Base64::base64_encode($string); echo "<br />"; echo base64_encode($string); ?> and the output will be: mCHCwQPwWFaqiWhQ9x0kSbdK4tgVjHEh // with custom mapping MchcWqpWwfAQIwHqSX0KsBDk4TGvJheH // the base64_encode() I need help how to decode the this key i know of Bace64 rot_13 is thare more than just this? (for urls) need help I need to know how to decode it <?php echo "\x61s\144as\x61s\x64"; ?> ---------------------------------- This is what was encoded: <?php echo "asdasasd"; ?> |