PHP - Json Decode Returning Null?
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. Similar TutorialsHey 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! 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 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 I 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 Hello
I am having an issue with encoding an array to JSON, my array seems to be formed correctly but when I try and echo the json_encode version I get "null". I echo the JSON error code and it is "0" indicating no issue..
I have read that non UTF-8 characters can be an issue, but this is simple data, no strange characters... but I added the utf8_encode anyway....
Here is my code:
$dataArray = array( 'title' => 'Test Item', 'icon_url' => 'http://url/api/testimage.jpg', 'item_id' => '12345', 'expiration' => 600 ); utf8_encode($dataArray); $responsArray = array( 'success' => '1', 'data' => $dataArray ); utf8_encode($responsArray); header('Content-type: application/json'); echo print_r($responsArray); echo json_encode ($responseArray); echo json_last_error(); Okay so I created a theme for my website that will also utilize a custom CMS script I developed and its not cooperating with me nicely. Here's my head of my header file for my template: Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>> <head> <title><?php bloginfo('name') ?>: <?php bloginfo('description') ?></title> <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" /> <meta http-equiv="content-type" content='<?php bloginfo("html_type"); ?> charset=<?php bloginfo('charset'); ?>' /> <?php if(is_singular()){ wp_enqueue_script('comment-reply');} ?> <?php wp_head(); ?> <?php require ('efedmanager/inc/dbconfig.php'); ?> </head> Now on my index page that calls the header file I DO NOT get a error saying that the file could not be located. So that's good news. Here's my dbconfig.php file. Code: [Select] <?php /** * @author Jeff Davidson * @copyright 2010 */ // This file contains the database access information and establishes a connection to MySQL and selects the database // Set the database access information as contstants DEFINE ('DB_USER', '?'); DEFINE ('DB_PASSWORD', '?'); DEFINE ('DB_HOST', '?'); DEFINE ('DB_NAME', '?'); // Make the database connection $dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (!$dbc) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); } ?> Now when I go to one of my custom pages like this one it brings up the message: Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/xtremer/public_html/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code on line 38 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/xtremer/public_html/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code on line 39 Code: [Select] <div id="champions" class="content"> <h1 class="pageheading">KOW Champions and Contenders</h1> <?php $championsQuery = " SELECT titles.titleName, titles.shortName, champions.champID, champions.con1ID, champions.con2ID, champions.con3ID, biochamp.shortName AS championshortName, biochamp.characterName AS champion, biocon1.shortName AS con1shortName, biocon1.characterName AS con1, biocon2.shortName AS con2shortName, biocon2.characterName AS con2, biocon3.shortName AS con3shortName, biocon3.characterName AS con3 FROM champions LEFT JOIN titles AS titles ON titles.ID = champions.titleID LEFT JOIN characters AS biochamp ON champions.champID = biochamp.ID LEFT JOIN characters AS biocon1 ON champions.con1ID = biocon1.ID LEFT JOIN characters AS biocon2 ON champions.con2ID = biocon2.ID LEFT JOIN characters AS biocon3 ON champions.con3ID = biocon3.ID WHERE titles.statusID = '1' ORDER BY titles.ID"; $championsResult = mysqli_query($dbc, $championsQuery); while ( $row = mysqli_fetch_array ( $championsResult, MYSQLI_ASSOC ) ) { $fieldarray=array('titleName','shortName','championID','championshortName','champion','con1ID','con1','con1shortName','con2ID','con2','con2shortName','con3ID','con3','con3shortName'); foreach ($fieldarray as $fieldlabel) { ${$fieldlabel} = $row[$fieldlabel]; } ?> <div id="title"><span class="large">< ?php echo $titleName ?></span> < ?php if (file_exists('images/championshots/'.$titleshortName.'/'.$championshortame.'.png')) { echo "<img class=champion src=images/championshots/".$shortName."/".$champion.".png alt= />\n"; } else { echo "<img class=champion src=images/championshots/".$shortName."/".$shortName.".png alt= />\n"; } if (strlen ($champion) < 1) { echo "<span class=medium>Vacant"; } else { echo "<span class=medium><a href=/bio?shortName=".$championshortName.">".$champion."</a></span>\n"; echo "<span class=medium>(Since TBD)</span>"; } ?> <span class="contender">Contenders</span> <ul> < ?php if ( strlen ($con1) < 1) { echo "<li><span class=medium>TBD</span>"; } else { echo "<li><a href=/bio?shortName=".$con1shortName.">".$con1."</a></li>\n"; } if (strlen ($con2) < 1) { echo "<li><span class=medium>TBD</span>"; } else { echo "<li><a href=/bio?shortName=".$con2shortName.">".$con2."</a></li>\n"; } if (strlen ($con3) < 1) { echo "<li><span class=medium>TBD</span>"; } else { echo "<li><a href=/bio?shortName=".$con3shortName.">".$con3."</a></li>\n"; } ?> </ul> </div> < ?php } ?> </div> Trying to figure out how to better write this chunk of code. I'm wanting to get the list of the roster members and then create an array of options for the view dropdown to display inside the select dropdown and also have it have an option to display "Please Select An Option". However what if what is returned from the getAllRoster function is NULL which is what I have returned if no results are returned from a query. How should I handle that which I just want the empty option displayed. Also I need to think about is do a function to retrieve all the allies for that specific matter and then display that ally as the default ally in the dropdown for each dropdown. Controller: Code: [Select] $rosterList = $this->bios->getAllRoster(); $allies = array(); $allies[''] = 'Please Select An Opion'; foreach ($rosterList AS $ally) { $allies[$ally->id] = $ally->rosterName; } View: Code: [Select] <?php echo form_label( 'Ally 1', 'ally1'); ?> <div> <?php echo form_dropdown( 'ally1', $allies, ''); ?> </div> <?php echo form_label( 'Ally 2', 'ally2'); ?> <div> <?php echo form_dropdown( 'ally2', $allies, ''); ?> </div> <?php echo form_label( 'Ally 3', 'ally3'); ?> <div> <?php echo form_dropdown( 'ally3', $allies, ''); ?> </div> Ok, this may be just because I have been programming all day and my mind has gone blank (happens alot), but this is my PHP script: Code: [Select] <?php $query_distinct_item_types = mysql_query("SELECT DISTINCT name FROM item_types"); while($item_types = mysql_fetch_array($query_distinct_item_types)){ $distinct_item_types[] = $item_types['name']; } foreach($distinct_item_types as $item){ $query_item_total = mysql_query("SELECT item_type, SUM(price) WHERE item_type='$item' FROM costs GROUP BY item_type"); while($item_total = mysql_fetch_array($query_total_price)){ $item_totals[] = $item_total['SUM(price)']; } } $item_summery = $item_totals; ?> $item_summery which is = to $item_totals is returning null, any idea's? Hello, I'm trying to take the value from an HTML form and insert it into a database on a button click. It inserts a null value into the database. The script is called submitColumnDetails.php. This is where I create the text field that I want to take the information from. This is in a separate file. Code: [Select] echo <<<END <form action = "submitColumnDetails.php" method = "POST"> <input type = "text" name = "columnField"/> </form> END; This is the submitColumnDetails.php file Code: [Select] <?php //Submit Column Data //-----------------------------------------------------// //Connect to localhost server $connector = mysql_connect("localhost", "root", "root"); if(!$connector){ //If user can't connect to database die('Could not connect: ' . mysql_error()); //Throw an error } //-----------------------------------------------------// mysql_select_db("colin_db", $connector); $newValue = $_POST["columnField"]; //Data from column field. THIS IS WHAT RETURNS NULL $newColumnQuery = "INSERT INTO `colin_db`.`allColumnFields` (`DATA`) VALUES ('$newValue')"; mysql_query($newColumnQuery); //Query to add form to main table $newColumnIntoMainTableQuery = "ALTER TABLE colin_table ADD ('$newValue' varchar(50))"; mysql_query($newColumnIntoMainTableQuery); //Query to add column to main table mysql_close($connector); //Close database connection echo "<script type = 'text/javascript'> window.location = 'index.php'</script>"; //Go back to original page ?> Even when I print out the $newValue, it does not print anything. What am I doing incorrectly? This topic has been moved to PHP Freelancing. http://www.phpfreaks.com/forums/index.php?topic=323713.0 Hi I am new to php, I am trying to capture the url and place into a variable but I only get the 1st digit to show, I just cant see what I am doing wrong. Sorry to ask such a basic question but I just can't work it out, I have attached a screen shot of all me code, your help would be very very much appreciated. I am trying to update the database with isset to set the value of the variable to either null or the value entered by the user. Currently, if no value has been entered null is being set as a string causing the date field to populate with zeros. How do I code it so that it populates or sets the value as null and not as string null?
Here's my code: (yes I know sql injections it's still in development )
<?php How to decode this? Code: [Select] http://www.mediafire.com/?niwske5oy33wzrz I need to know how to decode it <?php echo "\x61s\144as\x61s\x64"; ?> ---------------------------------- This is what was encoded: <?php echo "asdasasd"; ?> 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 Hi, Im trying to use base64_decode on a string, however the string is sometimes plain text others its encoded, how can i test to see if the sting is encoded or plaintext and only perform the decode if the string is indeed encoded? Im pretty new to PHP, but ive seen mentions of regex or preg_match for certain characteristics of base64 encoded, my one observation is it usually ends in == which wouldnt appear in my plain text, could i do a preg match for that? need help i know of Bace64 rot_13 is thare more than just this? (for urls) 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 |