magento api 首次接触 (-)
1 /** 2 * magento Api 身份验证 调用示例 3 * Example of simple PRoduct POST using Admin account via Magento REST API. OAuth authorization is used 4 */ 5 $callbackUrl = "http://127.0.0.1/oauth_admin.php";// 回调页面 7 $temporaryCredentialsRequestUrl = "http://www.magento.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl); //请求Url 获取临时凭证 8 $adminAuthorizationUrl = 'http://www.magento.com/admin/oauth_authorize'; //请求授权 9 $accessTokenRequestUrl = 'http://www.magento.com/oauth/token'; //获取令牌 10 $apiUrl = 'http://www.magento.com/api/rest'; // magento 接口访问所有URL 都基于 此URL 11 $consumerKey = 'xfn5po1semt9iy680wdpcfuue06p058y'; 12 $consumerSecret = 'eq1x5n9w0ppwq4o3imf8b4xls81fh77d'; 13 session_start(); 14 if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) { 15 $_SESSION['state'] = 0; 16 } 17 try { 18 $authType = (@$_SESSION['state'] == 2) ? @OAUTH_AUTH_TYPE_AUTHORIZATION : @OAUTH_AUTH_TYPE_URI; 19 $oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType); 20 $oauthClient->enableDebug(); 21 22 if (!isset($_GET['oauth_token']) && !@$_SESSION['state']) { 23 $requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl); 24 $_SESSION['secret'] = $requestToken['oauth_token_secret']; 25 $_SESSION['state'] = 1; 26 //print_r($_SESSION);exit; 27 header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']); 28 exit; 29 } else if (@$_SESSION['state'] == 1) { 30 $oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']); 31 $accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl); 32 if(empty($accessToken)) { 33 exit("Failed fetching access token, response was: " . $oauthClient->getLastResponse()); 34 } 35 $_SESSION['state'] = 2; 36 $_SESSION['token'] = $accessToken['oauth_token']; 37 $_SESSION['secret'] = $accessToken['oauth_token_secret']; 38 header('Location: ' . $callbackUrl); 39 exit; 40 } else { 41 42 /***************************修改数据:************************/ 43 /*$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']); 44 $resourceUrl = "$apiUrl/products/1"; 45 $productData = json_encode(array( 46 'type_id' => 'simple', 47 'attribute_set_id' => 4, 48 'sku' => 'chujiu123' . uniqid(), 49 'weight' => 1, 50 'status' => 1, 51 'visibility' => 4, 52 'name' => 'chujiu update demo6 28', 53 'description' => 'chujiu Description 28', 54 'short_description' => 'chujiu Short Description 28', 55 'price' => 28.28, 56 'tax_class_id' => 0, 57 )); 58 $headers = array('Content-Type' => 'application/json'); 59 $oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_PUT, $headers); 60 print_r($oauthClient->getLastResponseInfo());*/ 61 62 63 /***************************添加数据:************************/ 64 65 /*$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']); 66 $resourceUrl = "$apiUrl/products"; 67 $productData = json_encode(array( 68 'type_id' => 'simple', 69 'attribute_set_id' => 4, 70 'sku' => 'chujiu123_cate3' . uniqid(), 71 'weight' => 1, 72 'weight' => 1, 73 'status' => 1, 74 'visibility' => 4, 75 'categories' => '2,3', 76 'name' => 'chujiu cate3 update demo2', 77 'description' => 'chujiu cate3 Description', 78 'short_description' => 'chujiu cate3 Short Description', 79 'price' => 59.95, 80 'tax_class_id' => 0, 81 )); 82 $headers = array('Content-Type' => 'application/json'); 83 $oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers); 84 print_r($oauthClient->getLastResponseInfo());*/ 85 86 87 /***************************分配产品到指定分类中************************/ 88 89 /*$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']); 90 $resourceUrl = "$apiUrl/products/9/categories"; //9 是产品的ID 91 $productData = json_encode(array( 92 'category_id' => 2, 93 )); 94 $headers = array('Content-Type' => 'application/json'); 95 $oauthClient->fetch($resourceUrl, $productData, OAUTH_HTTP_METHOD_POST, $headers); 96 print_r($oauthClient->getLastResponseInfo());*/ 97 98 99 /***************************查询: 获取数据:************************/100 101 $oauthClient->setToken(@$_SESSION['token'], @$_SESSION['secret']);102 $resourceUrl = "$apiUrl/products"; //如果获取单个商品的信息 $apiUrl/products/1 代表访问 ID 为1103 $oauthClient->fetch($resourceUrl);104 $productsList = json_decode($oauthClient->getLastResponse(), true);105 echo '<pre>';106 print_r($productsList);107 $k = array_keys($productsList[1]);108 $keys = join(',', $k);109 echo $keys; // 添加商品的字段110 111 /***************************删除数据:************************/112 113 /*$oauthClient->setToken(@$_SESSION['token'], @$_SESSION['secret']);114 $resourceUrl = "$apiUrl/products/4";115 $oauthClient->fetch($resourceUrl, OAUTH_HTTP_METHOD_DELETE);116 echo '<pre>';117 print_r($oauthClient->getLastResponseInfo());*/118 }119 } catch (OAuthException $e) {120 echo '<pre>';121 print_r($e);122 }
新闻热点
疑难解答