php 示例PHP在Magento 1上连接API REST

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 示例PHP在Magento 1上连接API REST相关的知识,希望对你有一定的参考价值。

<?php
// Created by Rafael Corrêa Gomes
// Reference http://devdocs.magento.com/guides/m1x/api/rest/introduction.html#RESTAPIIntroduction-RESTResources

// Custom Resource
$apiResources = "products?limit=2";

// Custom Values
$isAdminUser = true;
$adminUrl = "admin";
$callbackUrl = "http://dev.local3.com/rest.php";
$host = 'http://dev.local3.com/magento/';

$consumerKey    = '7e1339ba397c917f00066fca13543934';
$consumerSecret = '6a3bc1999d9399c655f0b28edbdb8610';


// Don't change
$temporaryCredentialsRequestUrl = $host . "oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = ($isAdminUser) ? $host . $adminUrl . "/oauth_authorize" : $host . "oauth/authorize";
$accessTokenRequestUrl = $host . "oauth/token";
$apiUrl = $host . "api/rest/";

session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
    $_SESSION['state'] = 0;
}
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']);
        $resourceUrl = $apiUrl.$apiResources;
        $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json', 'Accept' => '*/*'));
        // $productsList = json_decode($oauthClient->getLastResponse());
        $productsList = $oauthClient->getLastResponse();
        // echo "<pre>";
        print_r($productsList);
        // echo "</pre>";
    }
} catch (OAuthException $e) {
    echo "<pre>";
    print_r($e->getMessage());
    echo "<br/>";
    print_r($e->lastResponse);
    echo "</pre>";
}

以上是关于php 示例PHP在Magento 1上连接API REST的主要内容,如果未能解决你的问题,请参考以下文章

[解决] Magento 2.1.3在PHP 7.1.0上

Magento2 php商城在windows10上安装

PHP 通过cms在magento页面上显示类别块

Magento,致命错误,“在非对象上调用 ... getAllOptions() ... Core\Model\Cache.php”

PHP 如何在每个请求上记录Magento中的所有xml布局更新

magento 的 php 的 intl 和 zip 包的问题