PHP stdClass nested properties - php

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);

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);

Why may Bit-Wasp using PrivateKeyFactory::fromWif PHP produce Deprecated ERROR

In my script this line of code using Bit-Wasp library for PHP gives me the following ERROR:
Deprecated: Non-static method BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory::fromWif() should not be called statically
What can be the problem? Searched Examples from the Lib and many other examples with Bit-Wasp none of it worked. ((
use BitWasp\Bitcoin\Bitcoin;
.......
$addrCreator = new AddressCreator();
$transaction = TransactionFactory::build()
->input('some UTXO', 0)
->payToAddress(1000, $addrCreator->fromString('some addres'))
->payToAddress(1000, $addrCreator->fromString('some addres'))
->get();
$hex = $transaction->getHex();
$privateKey = 'WIF PRiVATE KEY';
$tx = TransactionFactory::fromHex($hex);
$utxos = [];
foreach ($tx->getInputs() as $idx => $input) {
$txid = $input->getOutPoint()->getTxId()->getHex();
$vout = $input->getOutPoint()->getVout();
$scriptPubKey = $input->getScript()->getBuffer()->getHex();
$utxo = new Utxo(new OutPoint(Buffer::hex($txid, 32), $vout), new TransactionOutput(0, ScriptFactory::fromHex($scriptPubKey)));
array_push($utxos, $utxo);
}
$priv = $factory->fromWif($privateKey);
$priv = PrivateKeyFactory::fromWif($privateKey);
$signer = new Signer($tx, Bitcoin::getEcAdapter());
foreach ($utxos as $i => $utxo) {
$signer->sign($i, $priv, $utxo->getOutput());
}
$signed = $signer->get();
echo $signed->getHex() . PHP_EOL;
PS all other functions from the Lib work fine.. just cannot figure out how sign raw tx using WIF private key ((
Any help is highly appreciated.
Thanks!
This is no tested code, just an explanation.
A static method is called on a class directly and not on an instance.
It looks like the library changed. So create an instance and call the method on it like:
$factory = new PrivateKeyFactory();
$key = $factory->fromWif($privateKey);

How to fix new stdClass error

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);
}

trying to use json_decode but it keeps returning a string instad of a object

I'm trying to access the information in a JSON string by using json_decode,
but it is returning a string instead of a object. My code
var_dump(json_decode($json)); // print json object to make sure it is working
$obj = json_decode($json); // get json object
print $obj->{'time'}; // 12345 // error because obj is a string
Try this below one instead of $obj = json_decode($json)
$obj = json_decode($json,true);
Ex:
$obj = '{"name":"vijay","age":27,"city":"New York"}';
$obj = json_decode($obj,true);
echo $obj["name"]; // vijay

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