Skip to content

Instantly share code, notes, and snippets.

@drewgillson
Created December 1, 2013 01:30
Show Gist options
  • Save drewgillson/7727507 to your computer and use it in GitHub Desktop.
Save drewgillson/7727507 to your computer and use it in GitHub Desktop.
<?php
/**
* Example of simple product POST using Admin account via Magento REST API. OAuth authorization is used
*/
$callbackUrl = "http://yourdomain.com/magento-create-product-rest-api.php"; // the URL of this file
$temporaryCredentialsRequestUrl = "http://yourdomain.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://yourdomain.com/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://yourdomain.com/oauth/token';
$apiUrl = 'http://yourdomain.com/api/rest';
$consumerKey = 'v3b7p1yn29q0378ybk9w5d7hrsmiifpf';
$consumerSecret = 'q91boucyygushav4ce4x07sohvpsynlg';
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,
));
$oauthClient->disableRedirects();
$headers = array('Content-Type' => 'application/json', 'Content_Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
$response = $oauthClient->getLastResponseInfo();
if (isset($response['url'])) {
header('Location: ' . $response['url']);
}
}
} catch (OAuthException $e) {
echo "<pre>" . print_r($e,true) . "</pre>";
}
@ashtech-krishna
Copy link

ashtech-krishna commented May 19, 2016

Hello,
I didn't find oauth class? From where I can access it?

@java2dev
Copy link

If I want to use it to integrate with prestashop, I have to change the key and token?

@mikebranderhorst
Copy link

@akimania
Copy link

there exists that code from anywhere on the internet but there is also 99999 million question for the problem about the error
Notice: Undefined index: state in .....
there is no answer, why published a code that does not work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment