PHP Server Error 500 when Echo doesnt exist? - php

My php code works fine when I add in an echo in a certain spot, but when I remove the simple echo "hello"; line it doesnt load the php page and it just shows Server Error 500.
The following code works:
require 'steam.php';
$steam = new steam();
$api = "FC1D74A835829EB99A571235A499638A";
$query = 'ari9';
$communityID = $steam->getID($query, $api);
$userInfo = $steam->getUserInfo($communityID, $api);
echo "hello";
$info = array(
'communityID' => $communityID,
'steamID' => $steam->$convertToSteamID($communityID),
'personaname' => $userInfo['personaname'],
'profileurl' => $userInfo['profileurl'],
'lastlogoff' => $userInfo['lastlogoff'],
);
foreach ($info as $key => $value) {
if (strlen($value)===0) {
$info[$key] = false;
}
echo $key." -> ".$value."<br>";
}
echo json_encode($info);
And the following code does not work:
require 'steam.php';
$steam = new steam();
$api = "FC1D74A835829EB99A571235A499638A";
$query = 'ari9';
$communityID = $steam->getID($query, $api);
$userInfo = $steam->getUserInfo($communityID, $api);
$info = array(
'communityID' => $communityID,
'steamID' => $steam->$convertToSteamID($communityID),
'personaname' => $userInfo['personaname'],
'profileurl' => $userInfo['profileurl'],
'lastlogoff' => $userInfo['lastlogoff'],
);
foreach ($info as $key => $value) {
if (strlen($value)===0) {
$info[$key] = false;
}
echo $key." -> ".$value."<br>";
}
echo json_encode($info);
and the following code does not work:
require 'steam.php';
$steam = new steam();
$api = "FC1D74A835829EB99A571235A499638A";
$query = 'ari9';
$communityID = $steam->getID($query, $api);
$userInfo = $steam->getUserInfo($communityID, $api);
$info = array(
'communityID' => $communityID,
'steamID' => $steam->$convertToSteamID($communityID),
'personaname' => $userInfo['personaname'],
'profileurl' => $userInfo['profileurl'],
'lastlogoff' => $userInfo['lastlogoff'],
);
foreach ($info as $key => $value) {
if (strlen($value)===0) {
$info[$key] = false;
}
echo $key." -> ".$value."<br>";
}
echo json_encode($info);
echo "Hello";
The first piece of code has the echo on line 9 and the second one is identical to the first except the echo on line 9 is removed. I'm puzzled by this.

The issue is that this $steam->$convertToSteamID is looking for a function with the name of whatever that variables value is. The reason you get the 500 when there isn't an echo is because when you send the echo that serves a 200. There should be events in your log displaying the error. You can't have 2 status codes though on a page.
So just change your function call to:
$steam->convertToSteamID
and you should be fine.

Related

An uncaught Exception was encountered php codeigniter

I want to array value push another single array value, how the single data push another array.
This my below code:
$this->db->select('emp_info.empid');
$this->db->from('emp_info');
$this->db->where("emp_info.empid", 2);
$this->db->where("emp_info.comcod", $compcod);
$this->db->where("emp_info.status", 1);
$empdata = $this->db->get();
$data_day = array();
$Dated1 = $this->model_employes->getDatesFromRange($fdate, $tdate);
foreach ($empdata->result() as $emp) {
foreach ($Dated1 as $key => $day) {
$dayid = date("Ymd", strtotime($day));
$data_day[] =
array(
'empid' => $emp->empid,
'shortcode' => '',
'attdate' => $day,
'dayid' => $dayid
);
}
}
$m_dayid = array();
foreach ($data_day() as $monthday) {
array_push($data_day, $monthday->dayid);
}
echo "<pre>";
print_r($m_dayid());
echo "</pre>";
Change the code as follows Instead of $data_day() into $data_day and $m_dayid() into $m_dayid
<?php
$m_dayid = array();
foreach ($data_day as $monthday) {
array_push($data_day, $monthday->dayid);
}
echo "<pre>";
print_r($m_dayid);
echo "</pre>";
?>

Want to get just two object in json

I am trying to make a code verifier with the following code. I've looked at the answers in StackOverflow but couldn't get the right result.
The code is working fine but I want to echo just two value.
validate.php page code is here:
if(isset($apKey) && isset($code)) {
$en = new En();
$Key = $apKey;
$Codes = $code;
if($Key == null || $Codes == null) {
echo json_encode(['data' => 'No Key or code found, Please try again']);
exit;
}
$response = $en->validate($Key,$Codes);
$result = json_decode($response);
$elements = json_encode(['data' => $result]);
echo $elements;
exit;
}
echo json_encode(['data' => 'No Key or code found, Please try again']);
exit;
Now I am trying to check this validate page results using this code:
$siteurl = urlencode($_SERVER['SERVER_NAME']);
$arrContextOptions = array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false
)
);
$file = file_get_contents('http://www.validates.com/validate.php?code=' . $check , false, stream_context_create($arrContextOptions));
$checks = json_decode($file, true);
$elements = $checks['data'];
echo print_r($elements);
So the results something like this:
Array
(
[buyer] => abcd
)
So what I want to do. I want to echo just buyer and error message after this code: $checks = json_decode($file, true);
The error result is here:
{"data":{"error":404,"description":"No sale belonging to the current user found with that code"}}
Like for example:
if(buyer){echo 'true';}
if(error message){echo 'error';}
I guess you can check with array_key_exists if there is error. You can do something like this:
$checks = json_decode($file, true);
$data = $checks['data'];
if (array_key_exists("error", $data))
echo $data["description"];
else
echo $data["buyer"];

Im trying to consume a WCF service with PHP but it does not return the result i want where do i go wrong

PHP CODE is below
$soap = new SoapClient
("http://www.eservices.gov.bw/TolisExternalService/TolisPostService.svc
?singleWsdl",
array('soap_version' => 'SOAP_1_2',
'location'=>'http://www.eservices.gov.bw/TolisExternalService/TolisPostService.svc'));
$params = array(
'licenceNo' => '2018/TEL/E/003'
);
$response= $soap->UpdateLevyPayment('$params');
$values = $response->GetAnnualFeeDueDetailsResult;;
var_dump($values);
WCF CODE
link for WCF service:http://www.eservices.gov.bw/TolisExternalService/TolisPostService.svc
You can try with this:
<?php
// SOAP
$soap = new SoapClient(
"http://www.eservices.gov.bw/TolisExternalService/TolisPostService.svc?singleWsdl",
array(
'soap_version' => 'SOAP_1_2',
'location'=>'http://www.eservices.gov.bw/TolisExternalService/TolisPostService.svc')
);
// List functions
echo 'Functions: '.'</br>';
$functions = $soap->__getFunctions();
foreach($functions as $item) {
echo $item.'</br>';
}
echo '</br>';
// List types
echo 'Types: '.'</br>';
$types = $soap->__getTypes();
foreach($types as $item) {
echo $item.'</br>';
}
echo '</br>';
// Consume SOAP
$params = array(
'licenceNo' => '2018/TEL/E/003'
);
$values = $soap->GetAnnualFeeDueDetails($params);
print_r($values);
?>
Notes: Tested with PHP 7.1

How to convert a multidimensional associative array to JSON?

As you can see in the JSON file, this looks not so necessary.
I get values from input-s and add it to the array and send it via AJAX. A simple array I know how to convert, but a multidimensional one is not. Can eat what that function? I tried to create an array with "keys", but there is a lot of trouble, I never reached the end , and I'm sure it's not right. Tell me what you can do.
i want this
{
"user1" : {
first_name":"Michael",
"last_name":"Podlevskykh",
"phones" : {
"phone_1":"5345",
"phone_2":"345345",
"phone_3":"123"
}
}
}
//this is what i see
JSON
[
{"first_name":"Michael"},
{"last_name":"Podlevskykh"},
[{"phone_1":"5345"},
{"phone_2":"345345"},
{"phone_3":"0991215078"}
]
]
PHP
//[["5345", "345345", "123"], "Michael", "Podlevskykh"]
$userInfo = (json_decode($_POST["phones"], true));
$namePhones = ["phone_1", "phone_2", "phone_3"];
$nameUser = ["first_name", "last_name"];
$jsonPhones = $userInfo;
$nameLName = $userInfo;
$jsonPhones = array_splice($jsonPhones, 0, 1);
$nameLName = array_splice($nameLName, -2);
foreach ($jsonPhones[0] as $key => $value) {
$phones[] = array($namePhones[$key] => $jsonPhones[0][$key]);
}
foreach ($nameLName as $key => $value) {
$usersName[] = array($nameUser[$key] => $nameLName[$key]);
}
array_push($usersName, $phones);
echo "<pre>";
echo json_encode($usersName);
//[
// {"first_name":"Michael"},{"last_name":"Podlevskykh"},
// [{"phone_1":"5345"},{"phone_2":"345345"},{"phone_3":"123"}]
//]
I don't get all the complications, I would do something like this if I'm sure the $input format is the same:
<?php
$input = '[["5345", "345345", "123"], "Michael", "Podlevskykh"]';
$input = json_decode($input, true);
$output = [
'user1' => [
'first_name' => $input[1],
'last_name' => $input[2],
'phones' => [
'phone_1' => $input[0][0],
'phone_2' => $input[0][1],
'phone_3' => $input[0][2]
]
]
];
echo '<pre>';
echo json_encode($output);
If you want an object as output, you need to create an object:
$userInfo = (json_decode($_POST["phones"], true));
$namePhones = ["phone_1", "phone_2", "phone_3"];
$nameUser = ["first_name", "last_name"];
$jsonPhones = $userInfo;
$nameLName = $userInfo;
$jsonPhones = array_splice($jsonPhones, 0, 1);
$nameLName = array_splice($nameLName, -2);
$user = new stdClass();
foreach ($nameLName as $key => $value) {
$user->{$nameUser[$key]} = $nameLName[$key];
}
$phones = new stdClass();
foreach ($jsonPhones[0] as $key => $value) {
$phones->{$namePhones[$key]} = $jsonPhones[0][$key];
}
$user->phones = $phones;
$users = new stdClass();
$users->user1 = $user;
echo json_encode($users);
Output:
{"user1": {
"first_name":"Michael",
"last_name":"Podlevskykh",
"phones":{
"phone_1":"5345",
"phone_2":"345345",
"phone_3":"123"
}
}
}

array result using foreach statement display in json in php

I am getting the result from web service SOAP client. I have created an array to get the result and display it using json format. I am getting few of my results properly. I have SerialEquipment parameter which is array and i need to get the result using foreach loop. I am doing an mistake there. I dont know how can i assign my $vehiclResult array in this for each statement. So that all the results at last i will collect and display using json using vehicleResult array.My mistake is in the foreach loop.
structure for SerialEquipment parameters:
Code:
$vehicle = getVehicleValuation();
$Serial=$vehicle['SerialEquipment'];
$vehiclResult = array(
'WE_Number' => $vehicle['WE Number'] ."<br>",
'Vehicle Type'=> $vehicle['Vehicle Type'] . "<br>",
'HSN' => $vehicle['HSN'] . "<br>",
'TSN' => $vehicle['TSN'] . "<br>"
);
foreach($Serial as $key => $obj) {
if(!isset($vehiclResult[$key]))
$vehiclResult[$key] = array();
$vehiclResult[$key]['SerialEquipment'] = $key. "<br>";
$vehiclResult[$key]['Code'] = $obj->Code. "<br>";
$vehiclResult[$key]['Desc Short'] = $obj->Desc_Short. "<br>";
$vehiclResult[$key]['Desc Long'] = $obj->Desc_Long. "<br>";
foreach($obj->Esaco as $key2 => $obj2) {
if($obj2->EsacoMainGroupCode === null){
// doesn't contain Esaco
continue;
}
else{
if(!isset($vehiclResult[$key][$key2]))
$vehiclResult[$key][$key2] = array();
$vehiclResult[$key][$key2]['esaco'] = $key2. "<br>";
$vehiclResult[$key][$key2]['EsacoMainGroupCode'] = $obj2->EsacoMainGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoMainGroupDesc'] = $obj2->EsacoMainGroupDesc. "<br>";
$vehiclResult[$key][$key2]['EsacoSubGroupCode'] = $obj2->EsacoSubGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoSubGroupDesc'] = utf8_decode($obj2->EsacoSubGroupDesc). "<br>";
$vehiclResult[$key][$key2]['EsacoGroupCode'] = $obj2->EsacoGroupCode. "<br>";
$vehiclResult[$key][$key2]['EsacoGroupDesc'] = utf8_decode($obj2->EsacoGroupDesc). "<br>";
}
}
}
$result = array(
'vehicle' => $vehiclResult
);
echo json_encode($result);
die();
}
You need to check if your array have the key so:
if(!isset($vehiclResult[$key]))
if not, you need to create it:
$vehiclResult[$key] = array(); // as an array
Also, you don't really need to make a description of your "item". You can Parse your JSON on the result page to output some text.
You can do something like.
Do something like:
foreach($Serial as $key => $obj) {
if(!isset($vehiclResult[$key]))
$vehiclResult[$key] = array();
$vehiclResult[$key]['serial'] = $key;
$vehiclResult[$key]['code'] = $obj->Code;
$vehiclResult[$key]['short_desc'] = $obj->Desc_Short;
$vehiclResult[$key]['long_desc'] = $obj->Desc_Long;
foreach($obj->Esaco as $key2 => $obj2) {
if($obj2->EsacoMainGroupCode === null){
// doesn't contain Esaco
continue;
}
else{
if(!isset($vehiclResult[$key][$key2]))
$vehiclResult[$key][$key2] = array();
$vehiclResult[$key][$key2]['esaco'] = $key2;
$vehiclResult[$key][$key2]['EsacoMainGroupCode'] = $obj2->EsacoMainGroupCode;
$vehiclResult[$key][$key2]['EsacoMainGroupDesc'] = $obj2->EsacoMainGroupDesc;
$vehiclResult[$key][$key2]['EsacoSubGroupCode'] = $obj2->EsacoSubGroupCode;
$vehiclResult[$key][$key2]['EsacoSubGroupDesc'] = utf8_decode($obj2->EsacoSubGroupDesc);
$vehiclResult[$key][$key2]['EsacoGroupCode'] = $obj2->EsacoGroupCode;
$vehiclResult[$key][$key2]['EsacoGroupDesc'] = utf8_decode($obj2->EsacoGroupDesc);
}
}
}
$result = array(
'vehicle' => $vehiclResult
);
echo json_encode($result);
die();
If you would keep your "text" and your <br> code, do the samething but add what you want to output after the "="
EDIT
** A HAVE CHANGE THE CODE PREVIOUSLY..
if you want to test your $vehiclResult, try something like:
foreach($vehiclResult as $key=>$value){
if(!is_array($value))
var_dump($value);
else {
foreach($value as $key2=>$value2){
if(!is_array($value2))
var_dump($value2);
else {
foreach($value2 as $key3=>$value3){
var_dump($value3);
}
}
}
}

Categories