Search code examples
phpjsonsoapsoap-clientvar-dump

PHP: Print one value from var_dump from API


I am working on an API call using PHP. The PHP script uses SOAP and JSON-RPC. It does work but I am having trouble getting only one value from the script to print instead of the entire data dump. From what I read, var_dump returns variable, type, value. I specifically only want to pull the value of one variable.

The end of my script is:

$jsonRpcRequest = array (
'method' => 'login',
'params' => array($merchantCode, $now, $hash),
'jsonrpc' => '2.0'
);
$ID = callRPC((Object)$jsonRpcRequest, $ApiUrl, true);
$productID = 4627344;
try {
$ProdID = $client->getProductById($ID, $productID);
} catch (SoapFault $e) {
echo "Product ID: " . $e->getMessage();
}
var_dump ($ProdID) ;

The script works as I get all the product data for this product id. However, I only want to print the value of price, not the entire dump.

script results:

object(stdClass)#2 (24) {["ProductId"]=> int(4627344)["ProductEnabled"]=> bool(true) ["ProductType"]=> string(7) "REGULAR" ["ProductVersion"]=> string(0) "" ["Price"]=> float(1450) ["Currency"]=> string(3) "USD"

I have tried using various examples from online but I cannot seem to only call the price.

The desired result: 1450


Solution

  • Just treat it like any normal object, use the -> arrow notation for accessing properties:

    $price = $ProdID->Price;
    echo $price;