2

I'm trying to run the Magento REST API example for fetching item information. This is the code I have:

<?php
/**
 * Example of products list retrieve using Customer account via Magento REST API. OAuth authorization is used
 */
$callbackUrl = "http://localhost/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://localhost/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://localhost/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://localhost/oauth/token';
$apiUrl = 'http://localhost/magento/api/rest';
$consumerKey = 'mykey';
$consumerSecret = 'mysecret';

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);
        $_SESSION['secret'] = $requestToken['oauth_token_secret'];
        $_SESSION['state'] = 1;
        header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
        exit;
    } 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);
        exit;
    } else {
        $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
        $resourceUrl = "$apiUrl/products";
        $oauthClient->fetch($resourceUrl);
        $productsList = json_decode($oauthClient->getLastResponse());
        print_r($productsList);
    }
} catch (OAuthException $e) {
    print_r($e);
}

Now, when I try to run this example using php <filename>.php, I get the following error:

PHP Notice:  Undefined index: state in /Desktop/magento.php on line 18
PHP Notice:  Undefined index: state in /Desktop/magento.php on line 22

I do not get any other output / errors. What is the problem here? When I try to access Magento using Java / Scribe, I'm able to get the access token. I'm having Magento CE 1.9.

Thanks.

1 Answer 1

3

It looks like this code sample was not meant to be run with php <filename>.php in command line, but in Web server context (for example, the use of $_GET means this script expects to get some query parameters).

If you don't have a Web server up and configured to place this script on, and assuming your PHP version is 5.4 or newer, you can run this using the built-in server, like so:

php -S localhost:8080 <filename>.php

Then access http://localhost:8080 with your browser and see if it works.

Note that the built-in Web server is useful for testing and development, but is not suited for production use.

2
  • It works, thanks. One more thing. Is it possible to use the same script to access 3rd party Magento services? I have asked a question at magento.stackexchange.com/questions/75507/… where I'm trying to access it from Java. Not sure if the problem is on my end or at the customer's end, so wanted to check with the php script as well. Jul 23, 2015 at 17:34
  • You probably can, as long as you set the URL and credentials correctly and that server has the API enabled and credentials set up. But I have not tried, so I cannot say for sure.
    – shevron
    Jul 24, 2015 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.