Search code examples
phpmagentomagento-1.9magento-rest-api

Magento REST API Example Errors


I'm trying to get a Magento REST API example working (Create a simple product as an Admin user with OAuth authentication) from the following link:

http://devdocs.magento.com/guides/m1x/api/rest/introduction.html

However I am receiving the following errors:

enter image description here

It's taken me quite some time to get this far after having to work out how to install OAuth and this part is becoming quite painful as I cannot find any solutions for these errors.

Does anybody have any ideas at all? My code is below if required:

<?php
$callbackUrl = "http://localhost/API.php";
$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 = '526ced0202719d14951e1849016d6b3d';
$consumerSecret = 'a06606b73962a0efbafea32af3d89380';

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";
        $productData = json_encode(array(
            'type_id'           => 'simple',
            'attribute_set_id'  => 4,
            'sku'               => 'simple' . uniqid(),
            'weight'            => 1,
            'status'            => 1,
            'visibility'        => 4,
            'name'              => 'Simple Product',
            'description'       => 'Simple Description',
            'short_description' => 'Simple Short Description',
            'price'             => 99.95,
            'tax_class_id'      => 0,
        ));
        $headers = array('Content-Type' => 'application/json');
        $oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
        print_r($oauthClient->getLastResponseInfo());
    }
} catch (OAuthException $e) {
    print_r($e);
}

Thank you for any insight.


Solution

  • Certificate issue fixed with adding $oauth->disableSSLChecks() to the PHP script.

    State issue fixed with with adding error_reporting(E_ALL | E_STRICT); to the PHP script.