How to fix new stdClass error - php

I am trying to understand the new stdClass in terms of sending data to a function, I've created the following code and I'll get an error that's saying Warning: Creating default object from empty value, however, still I get the json data as well. How to fix the error and why I am getting it?
$send = new stdClass();
$send->user->id = '12121212121';
$send->message->attachment->type = 'file';
$send->message->attachment->image = 'png';
$res = process($send);
function process($send){
$data = json_encode($send);
print_r($data);
}
Result is looks like this and there is an error above below result as I mentioned it:
{"user":{"id":"12121212121"},"message":{"attachment":{"type":"file","image":"png"}}}

Create stdClass for each reference like this
$send = new stdClass();
$send->user=new stdClass();
$send->user->id = '12121212121';
$send->message=new stdClass();
$send->message->attachment=new stdClass();
$send->message->attachment->type = 'file';
$send->message->attachment->image = 'png';
$res = process($send);
function process($send){
$data = json_encode($send);
print_r($data);
}

Related

Is there a way to get datatype long php?

Can I convert my int datatype variable to longint datatype because I need the object type to be long for soap API (XML)?
To convert PHP variables/arrays and object you can use SoapVar
to know further about SoapVar
for your task you need something like
$workOrder['ID'] = new SoapVar($data['ID'],XSD_LONG);
to convert PHP object into Soap Object follow the below code, hopefully it will help you in solving your problem
$workOrder = [];
$workOrder['ID'] = new SoapVar($data['ID'],XSD_LONG);
$note = [];
$note['body'] = "valid note body";
$note['date'] = new SoapVar("2022-07-18",XSD_DATETIME);
$note['private'] = true;
$note['subject'] = "valid note subject";
$varNote = new SoapVar($note, SOAP_ENC_OBJECT, 'ns3:Note', null);
$varWorkOrder = new SoapVar($workOrder, SOAP_ENC_OBJECT, 'ns3:WorkOrder', null);
try{
$response2 = $client->attachNoteToWorkOrder($varNote,$varWorkOrder);
}catch (\SoapFault $exception){
// dd($exception);
dd($client->__getLastRequest());
}
dd($response2);

PHP stdClass nested properties

This used to work for me but it no longer does. What's the new / better methodology for this?
$myObj = new stdClass();
$myObj->foo->bar = "content";
$payload = json_encode($myObj);
Now I get:
Uncaught Error: Attempt to modify property "bar" on null
You need to create the nested object explicitly.
$myObj = new StdClass;
$myObj->foo = new StdClass;
$myObj->foo->bar = "content";
$payload = json_encode($myObj);
But if you're just creating JSON, it would be simpler to use associative arrays instead of objects. They can be written as literals easily.
$myArr = ['foo' => ['bar' => "content"]];
$payload = json_encode($myArr);

Error while loading xml file?

Am dynamically loading an xml file and sending request to the api but getting
Warning: DOMDocument::loadXML(): Empty string supplied as input in /home/spotrech/public_html/ but this error is very inconsistent sometime appear sometime don't! I really no idea how to solve this. below is code
$rechargeApiUrl = "http://allrechargeapi.com/apirecharge.ashx?uid=$uid&apikey=$apike&number=$mobileNo&opcode=$opId&amount=$amount&ukey=$uniId&format=xml";
$url = file_get_contents($rechargeApiUrl);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML(preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $url));
$itemInfo = $xmlDoc->getElementsByTagName('Result'); //returns an object.
$itemCount = $itemInfo->length;
foreach ($itemInfo as $userInfo) {
//Assigning node values to its specified variables.
$ukey = strtolower($userInfo->getElementsByTagName('ukey')->item(0)->childNodes->item(0)->nodeValue);
$status = $userInfo->getElementsByTagName('status')->item(0)->childNodes->item(0)->nodeValue;
$resultCode = $userInfo->getElementsByTagName('resultcode')->item(0)->childNodes->item(0)->nodeValue;
}
$strStatus = strtolower(trim($status));
$strResultCode = trim($resultCode);
$strCode = trim($ukey);
any response will be appreciated.Thank you

PHP-EWS fails on more than one attachment

I use James Armes's PHP-EWS library.
The following code works fine with single attachments, but fails with multiply files.
<?php
$msgRequest->MessageDisposition = 'SaveOnly';
$msgResponse = $ews->CreateItem($msgRequest);
$msgResponseItems = $msgResponse->ResponseMessages->CreateItemResponseMessage->Items;
// Create attachment(s)
$attachments = array();
$i = 0;
foreach ($message_details['attachment'] as $attachment) {
$attachments[$i] = new EWSType_FileAttachmentType();
$attachments[$i]->Content = file_get_contents($attachment['path'] . '/' . $attachment['file']);
$attachments[$i]->Name = $attachment['file'];
$i++;
}
//
// Attach files to message
$attRequest = new EWSType_CreateAttachmentType();
$attRequest->ParentItemId = $msgResponseItems->Message->ItemId;
$attRequest->Attachments = new EWSType_NonEmptyArrayOfAttachmentsType();
$attRequest->Attachments->FileAttachment = $attachments;
$attResponse = $ews->CreateAttachment($attRequest);
$attResponseId = $attResponse->ResponseMessages->CreateAttachmentResponseMessage->Attachments->FileAttachment->AttachmentId;
// Save message id from create attachment response
$msgItemId = new EWSType_ItemIdType();
$msgItemId->ChangeKey = $attResponseId->RootItemChangeKey;
$msgItemId->Id = $attResponseId->RootItemId;
// Send and save message
$msgSendRequest = new EWSType_SendItemType();
$msgSendRequest->ItemIds = new EWSType_NonEmptyArrayOfBaseItemIdsType();
$msgSendRequest->ItemIds->ItemId = $msgItemId;
$msgSendRequest->SaveItemToFolder = true;
$msgSendResponse = $ews->SendItem($msgSendRequest);
$response = $msgSendResponse->ResponseMessages->SendItemResponseMessage;
?>
$ews->SendItem() returns this error:
Uncaught SoapFault exception: [a:ErrorSchemaValidation] The request
failed schema validation: The required attribute 'Id' is missing.
What do I miss here?
Found the answer here:
https://github.com/jamesiarmes/php-ews/issues/132
Basically Exchange does not use an array if there is only one attachment, so an additional check is required to determine where to get the ID from.
if(!is_array($attResponse->ResponseMessages->CreateAttachmentResponseMessage))
$attResponseId = $attResponse->ResponseMessages->CreateAttachmentResponseMessage->Attachments->FileAttachment->AttachmentId;
else {
$attResponseId = $attResponse->ResponseMessages->CreateAttachmentResponseMessage[0]->Attachments->FileAttachment->AttachmentId;
}
Exchange uses the same structure with recipients. I find this inconsistent, however I am sure there is a reason behind it.
I hope someone will benefit from raising this.

how can call a asmx webservice by parsing an object as parameter,

Here is my code
$objLogParameter = new LogParameters();
$objLogParameter->strLogMessage = $message."&nbsp".$extendedMessage;
$objLogParameter->strStackTrace = $preStackTrace;
$objLogParameter->strUser = "Osmosys";
$objLogParameter->strCustomer = "ws";
$objLogParameter->strPageOrModuleName = "calling ws";
$objLogParameter->strApplication = "OsmTest";
$objLogParameter->strSubscription = "test2";
$objLogParameter->EnumSeverity = "Error";
$objLogParameter->EnumLogType = "ErrorTest";
$url = "http://log.cocoonit.in/writelogsindbservice.asmx?WSDL";
$client = new SoapClient($url, array("trace" => 1));
$res = $client->WriteLogInDB($objLogParameter);
WriteLogInDB is a method in .asmx webservice, thats method except an object as a argument then if i pass a object in $res i got a msg like status code = 0,object couldn't be empty.
I want to connect this Url
http://log.cocoonit.in/writelogsindbservice.asmx
and in this it have a WriteLogInDB method, i want to call this method by passing an object nothing but $objLogParameter. how can i do it in php.
Please help me.

Categories