accessing data in php object
Iyzipay\Model\ThreedsInitialize Object ( [htmlContent:Iyzipay\Model\ThreedsInitialize:private] => [status:Iyzipay\IyzipayResource:private] => failure [errorCode:Iyzipay\IyzipayResource:private] => 5251 [errorMessage:Iyzipay\IyzipayResource:private] => Banka kartlar?� i?�in taksit yap?�lamaz [errorGroup:Iyzipay\IyzipayResource:private] => [locale:Iyzipay\IyzipayResource:private] => tr [systemTime:Iyzipay\IyzipayResource:private] => 1628696035608 [conversationId:Iyzipay\IyzipayResource:private] => 123456789 [rawResult:Iyzipay\ApiResource:private] => {"status":"failure","errorCode":"5251","errorMessage":" Banka kartlar?� i?�in taksit yap?�lamaz","locale":"tr","systemTime":1628696035608,"conversationId":"123456789"} )
how can i access 'errorMessage' in this object
Answer
Solution:
Not sure but Assuming the output you provided is string that comes from requesting another data source; You can use JSON field extraction regex like this. Note that $responseString
is the response body like the one you had in question.
$re = '/"errorMessage":"((\\\\"|[^"])*)"/i';
preg_match($re, $responseString, $matches, PREG_OFFSET_CAPTURE, 0);
$errorMessage = $matches[1][0];
var_dump($errorMessage);
For example for the provided response in question the result would be:
string(40) " Banka kartlar?� i?�in taksit yap?�lamaz"
BE AWARE In case if there was no matching for regex it will throw Undefined offset
.