Search code examples
phparraysmagento

Displaying a Value from a Variable - API (Magento)


I have an API script which is retrieving product data from the Magento API.

enter image description here

This is then displayed with the line print_r($productsList), which displays as so:

enter image description here

What I am trying to achieve is to echo only one of those values. I've tried things such as:

echo $productsList['sku'] and print_r(&productsList['sku'])

But nothing works. I assume this is because this is a variable and not an array. Is there any way of doing this? Or is the data being brought back from the Magento API in "one piece" which can't be split?

Full script is below.

<?php
error_reporting(E_STRICT);
$callbackUrl = "xxx";
$temporaryCredentialsRequestUrl = "https://ts564737-container.zoeysite.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'https://ts564737-container.zoeysite.com/admin/oauth_authorize';
$accessTokenRequestUrl = 'https://ts564737-container.zoeysite.com/oauth/token';
$apiUrl = 'https://ts564737-container.zoeysite.com/api/rest';
$consumerKey = 'xxx';
$consumerSecret = 'xxx';

session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
    $_SESSION['state'] = 0;
}
try {
    
    $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
    
    $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
    $oauthClient->enableDebug();
    
    if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
        $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
        //echo '<pre>';print_r($requestToken);echo '</pre>';
        //exit;
        $_SESSION['secret'] = $requestToken['oauth_token_secret'];
        $_SESSION['state'] = 1;
        header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
    } else if ($_SESSION['state'] == 1) {
        $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
        $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
        $_SESSION['state'] = 2;
        $_SESSION['token'] = $accessToken['oauth_token'];
        $_SESSION['secret'] = $accessToken['oauth_token_secret'];
        header('Location: ' . $callbackUrl);
    } else {
        $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
        $resourceUrl = "$apiUrl/products/?order=entity_id&filter[0][attribute]=sku&filter[0][in][0]=product1";
        //echo $resourceUrl;
        //exit;
        $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => 'application/json'));
        $productsList = json_decode($oauthClient->getLastResponse());
    }
} catch (OAuthException $e) {
    echo '<pre>';print_r($e);echo '</pre>';
}

print_r($productsList);

?>

Solution

  • You can access it

    echo $productsList->215->sku
    

    In your case