Gewusst wie: implementieren von benutzerdefinierten REST-API in magento?

Ich versuche, um meine custom-REST-api.
Ich habe meine eigenen erstellen-Modul der Gewohnheits - /Restapi.Custom [Namespace], Restapi[Modul-name].

In etc Ordner habe ich auch erstellt config.xml und api2.xml. Unten ist der code:

Config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Restapi>
            <version>0.1.0.0</version>
        </Custom_Restapi>
    </modules>
    <global>
        <models>
            <restapi>
                <class>Custom_Restapi_Model</class>
            </restapi>
        </models>
</config>

api2.xml

<config>
    <api2>
        <resource_groups>
            <restapi translate="title" module="Custom_Restapi">
                <title>Custom Rest API</title>
                <sort_order>10</sort_order>
            </restapi>
        </resource_groups>
        <resources>
            <restapi translate="title" module="Custom_Restapi">
                <group>restapi</group>
                <model>restapi/api2_restapi</model>
                <title>Testing My Rest API</title>
                <sort_order>10</sort_order>
                <privileges>
                    <admin>
                        <create>1</create>
                       <!-- <retrieve>1</retrieve>
                        <delete>1</delete>-->
                    </admin>
                   <!--  <customer>
                        <create>1</create>
                        <retrieve>1</retrieve>
                         <delete>1</delete>
                    </customer>
                     <guest>
                        <create>1</create>
                       <retrieve>1</retrieve>
                        <delete>1</delete>
                    </guest>-->
                </privileges>
                <routes>
                    <route_entity>
                        <route>/custom/createwebsite/:s</route>
                        <action_type>entity</action_type>
                    </route_entity>
                </routes>
                <versions>1</versions>
            </restapi>
        </resources>
    </api2>

Model Directory-Struktur

app\code\local\Custom\Restapi\Model\Api2\Restapi.php. Unten ist der code der Datei:

Restapi.php

class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource
{

}

app\code\local\Custom\Restapi\Model\Api2\Restapi\Rest\Admin\V1.php Unten ist der code der Datei:

V1.php

class Custom_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Custom_Restapi_Model_Api2_Restapi
{
     protected function _create(){


   return  json_encode(array("testing","hello"));
}

 protected function _retrieveCollection()
{
      return  json_encode(array("testing","hello"));
 }
}

Etc/Modul-Konfigurations-Datei ist auch setup.

Admin-Einstellung

  1. Habe ich erstellen OAuth-Rolle admin. Unter der linken Seite der Registerkarte "Rolle Api-Ressource" Modul-Einstellungen sichtbar sind und ausgewählt.

  2. Rest Consumer-Einstellung ist auch konfiguriert.

Unten ist der REST-API aufrufende Skript-code-:

Api-Aufrufende Script-Code

$consumerKey    = 'ozr74egldg07dpxtkk9uq1o8bj6wwd65'; //from Admin Panel's &quot;REST - OAuth Consumers page&quot;
$consumerSecret = 'ozr74egldg07dpxtkk9uq1o8bj6wwd65'; //from Admin Panel's &quot;REST - OAuth Consumers page&quot;

//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/testscript.php";

//Set the URLs below to match your Magento installation
$temporaryCredentialsRequestUrl = "http://127.0.0.1/mage_restapi/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://127.0.0.1/mage_restapi/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://127.0.0.1/mage_restapi/oauth/token';
$apiUrl = 'http://127.0.0.1/mage_restapi/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']);



        //Generate coupon codes via POST
        $resourceUrl = "$apiUrl/custom";

        $oauthClient->fetch($resourceUrl, OAUTH_HTTP_METHOD_POST, array(
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
        ));


         $data= json_decode($oauthClient->getLastResponse(), true);


        echo "Data is:<br/>".$data;

    }
} catch (OAuthException $e) {

    print_r($e->getMessage());
    //echo "<br/>";
    //print_r($e->lastResponse);
}

Wenn ich versuche, Zugriff auf die API dann Fragen Sie nach Autorisieren

Autorisieren Anwendung admin-Anfragen Zugriff auf Ihr Konto

Nach der Autorisierung Anwendung Zugriff auf Ihr Konto.

autorisieren-button und die Schaltfläche "ablehnen"

Nach Klick auf "aktivieren" Fehler:

Ungültiger auth/bad request (bekam einen 404, erwartet HTTP/1.1 20X oder ein redirect)
{"messages":{"error": [{"- code":404,"message":"auf Anfrage nicht mit jeder route."}]}}

PHP OAuth-Erweiterung nicht unterstützt RSA-SHA1 Unterstützung in meinem OAuth-Einstellungen

Referenz-Link
http://www.magentocommerce.com/knowledge-base/entry/how-to-use-extend-the-magento-rest-api-to-use-coupon-auto-generation

http://ctodilemma.com/2013/04/customising-and-extending-the-magento-rest-api/

Verwenden wir nur die Funktion von magento für den Zugriff auf den code in Ruhe an, unten sind die Funktion Liste-:

  1. _create()
  2. _retrieve()
  3. _delete()
  4. _retrieveCollection()
  5. _update()
  6. _multiUpdate()
  7. _multiDelete

Fehler
Ungültiger auth/bad request (bekam einen 404, erwartet HTTP/1.1 20X oder ein redirect)
{"messages":{"error": [{"- code":404,"message":"auf Anfrage nicht mit jeder route."}]}}

Ich Sprach über meine Analyse, aber ich bin nicht in der Lage auf die Daten zugreifen.Bitte teilen Sie Ihr feedback.

Schreibe einen Kommentar