· 9 years ago · Nov 30, 2016, 01:48 PM
1<?php
2$callbackUrl = "http://localhost/API.php";
3$temporaryCredentialsRequestUrl = "https://ts564737-container.zoeysite.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
4$adminAuthorizationUrl = 'https://ts564737-container.zoeysite.com/admin/oauth_authorize';
5$accessTokenRequestUrl = 'https://ts564737-container.zoeysite.com/oauth/token';
6$apiUrl = 'https://ts564737-container.zoeysite.com/api/rest';
7$consumerKey = '526ced0202719d14951e1849016d6b3d';
8$consumerSecret = 'a06606b73962a0efbafea32af3d89380';
9
10session_start();
11if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
12 $_SESSION['state'] = 0;
13}
14try {
15 $authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
16 $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
17 $oauthClient->enableDebug();
18
19 if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
20 $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
21 $_SESSION['secret'] = $requestToken['oauth_token_secret'];
22 $_SESSION['state'] = 1;
23 header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
24 exit;
25 } else if ($_SESSION['state'] == 1) {
26 $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
27 $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
28 $_SESSION['state'] = 2;
29 $_SESSION['token'] = $accessToken['oauth_token'];
30 $_SESSION['secret'] = $accessToken['oauth_token_secret'];
31 header('Location: ' . $callbackUrl);
32 exit;
33 } else {
34 $oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
35 $resourceUrl = "$apiUrl/products";
36 $productData = json_encode(array(
37 'type_id' => 'simple',
38 'attribute_set_id' => 4,
39 'sku' => 'simple' . uniqid(),
40 'weight' => 1,
41 'status' => 1,
42 'visibility' => 4,
43 'name' => 'Simple Product',
44 'description' => 'Simple Description',
45 'short_description' => 'Simple Short Description',
46 'price' => 99.95,
47 'tax_class_id' => 0,
48 ));
49 $headers = array('Content-Type' => 'application/json');
50 $oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers);
51 print_r($oauthClient->getLastResponseInfo());
52 }
53} catch (OAuthException $e) {
54 print_r($e);
55}