PHP - Echo error mesasge if function dont found any XML



i have some easy script to get XML based on search input , convert XML to array and working with data in that array. Problem is that i am getting XML with function which is not on my PC get_details() and i want to let my user know if that function dont find any results based on vin they add to search input



public function searchByService(){
echo $this->vin;
try {
$client = new SOAPClient ( 'https://LINK' ); // initiate new SoapClient
$password ['_'] = 'My Password'; // password for authenticate_user function in SoapHeader
$encoded = new SoapVar ( $password, SOAP_ENC_OBJECT ); // make SoapVariable out of $password
$header = new SoapHeader ( 'http://ift.tt/1C5pQFZ', 'authenticate_user', $encoded ); // put authenticate_user method, and password in header
$client->__setSoapHeaders ( $header ); // set SoapHeader
$response = $client->get_details ($this->vin); // calling get_details with the vin given in the form field
if (isset($response)) {
$xml = simplexml_load_string ( $response ); // converting the response string to xml
$json = json_encode ( $xml ); // converting to an array in to easy steps (step 1)
$array = json_decode ( $json, TRUE ); // step 2
}
else echo "We didnt find any result for VIN ".$this->vin."";
} catch ( SoapFault $exception ) {
echo $exception;
}

if (isset ( $array ['result'] )) { //check for result and work with array
$array ['result'] ['claim'] = isset ( $array ['result'] ['claim'] [0] ) ? $array ['result'] ['claim'] : array ($array ['result'] ['claim']);
foreach ( $array ['result'] ['claim'] as $claim ) {
echo 'Creation: ' . $claim['creation'];
echo '<br>';
echo 'Country: ' . $claim ['country'];
echo '<br>';
echo 'Labor cost: ' . $claim ['specific']['laborCost'];
echo '<br>';
echo 'Paint cost: ' . $claim ['specific']['paintCost'];


As you can see i try to check if $response isset and hope it will work but i just var_dump it and noticed that there is this in it: string(202) " VF6SFR11200015182 some_uniqe_key "


So my question again: Is there a way how to display user error message like "Sorry, we didnt find any result for your VIN" ?


No comments:

Post a Comment