0

I am working on REST Api for saving the data,

i am trying as like below data is saving but can't return the response

The below file V1.php (model/Api2/Customer/Rest/Admin/V1.php)

protected function _create($couponData)
{
 $websiteId = Mage::app()->getWebsite()->getId();
 $store = Mage::app()->getStore();
 Mage::log($websiteId,null,'website.log');
 Mage::log($store,null,'store.log');
 try {
 $customer = Mage::getModel("customer/customer");
 $customer->setWebsiteId($websiteId)
 ->setStore($store)
 ->setFirstname($couponData['name'])
 ->setLastname($couponData['name'])
 ->setEmail($couponData['email'])
 ->setPassword($couponData['pwd']);
 $cid = $customer->save();
 $cid = "1232";
 echo "-------"; print_r($cid); echo "-------";
 // $this->_retrieveCollection($cid);
 //$this->_loadSalesRule($cid);
 return "testt";
 } catch(Exception $e) {
 echo "---------Going To Expection---------";
 print_r(json_encode($e->getMessage()));
 }
 return "testtt1";
}
/**
 * Retrieve list of coupon codes.
 *
 * @return array
 */
protected function _retrieveCollection($cid)
{
 /* $ruleId = $this->getRequest()->getParam('rule_id');
 $rule = $this->_loadSalesRule($ruleId);
 $collection = Mage::getResourceModel('salesrule/coupon_collection');
 $collection->addRuleToFilter($rule);
 $this->_applyCollectionModifiers($collection);
 $data = $collection->load()->toArray();
 //return $data['items'];*/
 return "----Recrive Collection Function---".$cid;
}
/**
 * Load sales rule by ID.
 *
 * @param int $ruleId
 * @return Mage_SalesRule_Model_Rule
 */
protected function _loadSalesRule($ruleId)
{
 /* 
 if (!$ruleId) {
 $this->_critical(Mage::helper('salesrule')
 ->__('Rule ID not specified.'), Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
 }
 $rule = Mage::getModel('salesrule/rule')->load($ruleId);
 if (!$rule->getId()) {
 $this->_critical(Mage::helper('salesrule')
 ->__('Rule was not found123.'), Mage_Api2_Model_Server::HTTP_NOT_FOUND);
 }
 return $rule;
 */
 return "Hello";
}

}

I am sending request with variable

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']);
 $ruleId = "3";
 $couponGenerationData = array();
 $couponGenerationData['name'] = "ramesh";
 $couponGenerationData['email'] = "[email protected]";
 $couponGenerationData['pwd'] = "test123";
 /* Send Throught Post*/
 $resourceUrl = $apiUrl."/coupondemo/{$ruleId}";
 //$resourceUrl = "$apiUrl/coupondemo/rules/{$ruleId}/codes";
 $oauthClient->fetch($resourceUrl, json_encode($couponGenerationData), OAUTH_HTTP_METHOD_POST, array(
 'Accept' => 'application/json',
 'Content-Type' => 'application/json',
 ));
 // Retrieve list of created coupons via GET
 $collectionFilters = array('limit' => $couponGenerationData['qty'], 'order' => 'coupon_id', 'dir' => 'dsc');
 $oauthClient->fetch($resourceUrl, $collectionFilters, OAUTH_HTTP_METHOD_GET, array(
 'Accept' => 'application/json',
 'Content-Type' => 'application/json',
 ));
 $coupons = json_decode($oauthClient->getLastResponse(), true);
 // Display the newly generated codes to demonstrate that the Coupon AutoGen API works
 // In reality, you might put these codes in emails to customers, store them in a database, etc.
 echo "New coupon codes:<br/>";
 foreach ($coupons as $coupon) {
 echo " --> " . $coupon['code'] . "<br/>";
 }
}} catch (OAuthException $e) {
print_r($e->getMessage());
echo "<br/>";
print_r($e->lastResponse);

}

it' returning only "Recrive Collection Function"

SNS
9251 gold badge13 silver badges33 bronze badges
asked Dec 30, 2016 at 13:14

1 Answer 1

0

In my above script i am sending POST & GET requests at same time that's why i am getting response for GET because GET is the latest(last) request.

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']);
 $ruleId = "3";
 $couponGenerationData = array();
 $couponGenerationData['name'] = "ramesh";
 $couponGenerationData['email'] = "[email protected]";
 $couponGenerationData['pwd'] = "test123";
/* Send Throught Post*/
$resourceUrl = $apiUrl."/coupondemo/{$ruleId}";
//$resourceUrl = "$apiUrl/coupondemo/rules/{$ruleId}/codes";
$oauthClient->fetch($resourceUrl, json_encode($couponGenerationData), OAUTH_HTTP_METHOD_POST, array(
 'Accept' => 'application/json',
 'Content-Type' => 'application/json',
));
$coupons = json_decode($oauthClient->getLastResponse(), true);
// Display the newly generated codes to demonstrate that the Coupon AutoGen API works
// In reality, you might put these codes in emails to customers, store them in a database, etc.
echo "New coupon codes:<br/>";
foreach ($coupons as $coupon) {
 echo " --> " . $coupon['code'] . "<br/>";
 }
 }
 } catch (OAuthException $e) {
 print_r($e->getMessage());
 echo "<br/>";
 print_r($e->lastResponse);
}
answered Jun 21, 2017 at 11:56
1
  • any idea about this error ? snag.gy/wTLXpx.jpg Commented Oct 19, 2018 at 6:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.