· 9 years ago · Nov 22, 2016, 07:08 AM
1$callbackUrl = "http://www.my-magento-store.com/oauth_admin.php";
2$temporaryCredentialsRequestUrl = "http://www.my-magento-store.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
3$adminAuthorizationUrl = 'http://www.my-magento-store.com/admin/oauth_authorize';
4$accessTokenRequestUrl = 'http://www.my-magento-store.com/oauth/token';
5$apiUrl = 'http://www.my-magento-store.com/api/rest';
6$consumerKey = '{Consumer Key}';
7$consumerSecret = '{Consumer Secret}';
8
9session_start();
10if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
11 $_SESSION['state'] = 0;
12}
13try {
14 $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
15 $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
16 $oauthClient->enableDebug();
17
18 if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
19 $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
20 $_SESSION['secret'] = $requestToken['oauth_token_secret'];
21 $_SESSION['state'] = 1;
22 header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
23 exit;
24 } else if ($_SESSION['state'] == 1) {
25 $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
26 $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
27 $_SESSION['state'] = 2;
28 $_SESSION['token'] = $accessToken['oauth_token'];
29 $_SESSION['secret'] = $accessToken['oauth_token_secret'];
30 header('Location: ' . $callbackUrl);
31 exit;
32 } else {
33 $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
34 $resourceUrl = "$apiUrl/products";
35 $productData = json_encode(array(
36 'type_id' => 'simple',
37 'attribute_set_id' => 4,
38 'sku' => 'simple' . uniqid(),
39 'weight' => 1,
40 'status' => 1,
41 'visibility' => 4,
42 'name' => 'My Product Name',
43 'description' => 'My Product Description',
44 'short_description' => 'My Products Short Description',
45 'price' => 6.99,
46 'tax_class_id' => 0,
47 ));
48 $headers = array('Content-Type' => 'application/json');
49 $oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
50 print_r($oauthClient->getLastResponseInfo());
51 }
52} catch (OAuthException $e) {
53 print_r($e);
54}
55
56$callbackUrl = "http://www.my-magento-store.com/oauth_customer.php";
57$temporaryCredentialsRequestUrl = "http://www.my-magento-store.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
58$adminAuthorizationUrl = 'http://www.my-magento-store.com/oauth/authorize';
59$accessTokenRequestUrl = 'http://www.my-magento-store.com/oauth/token';
60$apiUrl = 'http://www.my-magento-store.com/api/rest';
61$consumerKey = '{Consumer Key}';
62$consumerSecret = '{Consumer Secret}';
63
64session_start();
65if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
66 $_SESSION['state'] = 0;
67}
68try {
69 $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
70 $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
71 $oauthClient->enableDebug();
72
73 if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
74 $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
75 $_SESSION['secret'] = $requestToken['oauth_token_secret'];
76 $_SESSION['state'] = 1;
77 header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
78 exit;
79 } else if ($_SESSION['state'] == 1) {
80 $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
81 $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
82 $_SESSION['state'] = 2;
83 $_SESSION['token'] = $accessToken['oauth_token'];
84 $_SESSION['secret'] = $accessToken['oauth_token_secret'];
85 header('Location: ' . $callbackUrl);
86 exit;
87 } else {
88 $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
89 $resourceUrl = "$apiUrl/products";
90 $oauthClient->fetch($resourceUrl);
91 $productsList = json_decode($oauthClient->getLastResponse());
92 print_r($productsList);
93 }
94} catch (OAuthException $e) {
95 print_r($e);
96}
97
98<?xml version="1.0"?>
99 <magento_api>
100 <data_item>
101 <entity_id>18</entity_id>
102 <type_id>simple</type_id>
103 <sku>SKU Number</sku>
104 <description>Your Product Description
105 </description>
106 <meta_keyword>Meta Keywords </meta_keyword>
107 <short_description>Short Description</short_description>
108 <name>Product Name</name>
109 <meta_title>Product Title</meta_title>
110 <meta_description>Meta Desciption</meta_description>
111 <regular_price_with_tax>Regular Price of the product </regular_price_with_tax>
112 <regular_price_without_tax>Price without Tax</regular_price_without_tax>
113 <final_price_with_tax>Final Price With Tax</final_price_with_tax>
114 <final_price_without_tax>Final Price without Tax</final_price_without_tax>
115 <is_saleable>1</is_saleable>
116 <image_url>Path of the product image</image_url>
117 </data_item>
118 </magento_api>