我已经实施了休息服务。我创建了api2.xml
。
我的api2.xml代码是:app/code/community/Test/Hellorestapi/etc/api2.xml
<?xml version="1.0"?>
<config>
<api2>
<resource_groups>
<helloapi translate="title" module="Test_Hellorestapi">
<title>HelloWorld API</title>
<sort_order>10</sort_order>
</helloapi>
</resource_groups>
<resources>
<helloapi translate="title" module="Test_Hellorestapi">
<group>helloapi</group>
<model>helloapi/api2_helloworld</model>
<title>Hello World Testing</title>
<sort_order>10</sort_order>
<privileges>
<admin>
<create>1</create>
<retrieve>1</retrieve>
</admin>
</privileges>
<attributes>
<hello>Hello World</hello>
</attributes>
<routes>
<route>
<route>/helloworld/test</route>
<action_type>collection</action_type>
</route>
</routes>
<versions>1</versions>
</helloapi>
</resources>
</api2>
我的V1.php代码是app/code/community/Test/Hellorestapi/Model/Api2/Helloworld/Rest/Admin/V1.php
: -
class Test_Hellorestapi_Model_Api2_Helloworld_Rest_Admin_V1 extends Test_Hellorestapi_Model_Api2_Helloworld
{
protected function _retrieveCollection()
{
return json_encode(array("testing","hello"));
}
protected function _multicreate(array $helloData)
{
print_r($helloData);die('in create');
return json_encode(array("testing","hello"));
}
protected function _delete(array $helloData)
{
print_r($helloData);die('haha');
return json_encode(array("testing","hello"));
}
}
我正在调用create方法请求的rest客户端是:
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
$consumerKey = 'wgyygksagan0wmmp0qn5d5e9l9zwxwbg'; // from Admin Panel's "REST - OAuth Consumers page"
$consumerSecret = 'i0ccmp1dasbzk1bb64wphhl88sjwmiyj'; // from Admin Panel's "REST - OAuth Consumers page"
// Set the OAuth callback URL to this script since it contains the logic
// to execute *after* the user authorizes this script to use the Coupon AutoGen API
$callbackUrl = "http://127.0.0.1/magento/hello_testing_rest.php";
// Set the URLs below to match your Magento installation
$temporaryCredentialsRequestUrl = "http://127.0.0.1/magento/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://127.0.0.1/magento/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://127.0.0.1/magento/oauth/token';
$apiUrl = 'http://127.0.0.1/magento/api/rest';
session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1)
{
$_SESSION['state'] = 0;
echo "try";
}
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 {
// We have the OAuth client and token. Now, let's make the API call.
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
$resourceUrl = "$apiUrl/helloworld/test?type=rest";
$helloData = json_encode(array(
'name' => 'hello world',
));
$headers = array('Accept' => 'application/json,*/*;q=0.8', 'Content-Type' => 'application/json');
$oauthClient->fetch($resourceUrl, $helloData, OAUTH_HTTP_METHOD_POST, $headers);
echo 'response<pre>';
print_r($oauthClient->getLastResponseInfo());
$data = json_decode($oauthClient->getLastResponse(), true);
echo "Data is:<br/>" . $data;
}
session_destroy();
} catch (OAuthException $e) {
print_r($e->getMessage());
//echo "<br/>";
//print_r($e->lastResponse);
}
现在在调用其余客户端脚本时,我收到错误
“无效的身份验证/错误请求(获得405,预期HTTP / 1.1 20X或a 重定向)”。
我认为输出会在“创建”中打印并打印hello world数组。我的问题是我在通过oauth发送POST请求时出错了。如何向“_multicreate方法”发送请求。