Php 如何连接到我自己站点的Magento REST API

Php 如何连接到我自己站点的Magento REST API,php,api,rest,magento,oauth,Php,Api,Rest,Magento,Oauth,我有一个新的Magento站点,在那里我没有对服务器的直接控制权,但有对该站点的管理员访问权。如果有人能解释我做错了什么和/或以最简单的方式连接到我自己的网站,我将不胜感激 我已按照配置了所有Web服务设置 我有消费者密钥和消费者机密。为了方便起见,我粘贴了Magento提供的示例PHP登录代码。我完全按照提供的方式使用它,而不是在我的域和凭据中使用它 <?php /** * Example of retrieving the products list using Admin acco

我有一个新的Magento站点,在那里我没有对服务器的直接控制权,但有对该站点的管理员访问权。如果有人能解释我做错了什么和/或以最简单的方式连接到我自己的网站,我将不胜感激

我已按照配置了所有Web服务设置

我有消费者密钥和消费者机密。为了方便起见,我粘贴了Magento提供的示例PHP登录代码。我完全按照提供的方式使用它,而不是在我的域和凭据中使用它

<?php
/**
 * Example of retrieving the products list using Admin account via Magento REST API. OAuth authorization is used
 * Preconditions:
 * 1. Install php oauth extension
 * 2. If you were authorized as a Customer before this step, clear browser cookies for 'yourhost'
 * 3. Create at least one product in Magento
 * 4. Configure resource permissions for Admin REST user for retrieving all product data for Admin
 * 5. Create a Consumer
 */
// $callbackUrl is a path to your file with OAuth authentication example for the Admin user
$callbackUrl = "http://yourhost/oauth_admin.php";
$temporaryCredentialsRequestUrl = "http://yourhost/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://yourhost/admin/oAuth_authorize';
$accessTokenRequestUrl = 'http://yourhost/oauth/token';
$apiUrl = 'http://yourhost/api/rest';
$consumerKey = 'yourconsumerkey';
$consumerSecret = 'yourconsumersecret';

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/products";
        $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json'));
        $productsList = json_decode($oauthClient->getLastResponse());
        print_r($productsList);
    }
} catch (OAuthException $e) {
    print_r($e->getMessage());
    echo "&lt;br/&gt;";
    print_r($e->lastResponse);
} 
它在这一行失败了:

$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
显然是因为我定义的$callbackUrl无效。那么,这里应该是什么?它可以是任何URL吗?它必须与站点位于同一服务器上吗

/**
 * Example of retrieving the products list using Admin account via Magento REST API. OAuth authorization is used
 * Preconditions:
 * 1. Install php oauth extension
 * 2. If you were authorized as a Customer before this step, clear browser cookies for 'yourhost'
 * 3. Create at least one product in Magento
 * 4. Configure resource permissions for Admin REST user for retrieving all product data for Admin
 * 5. Create a Consumer
 */
// $callbackUrl is a path to your file with OAuth authentication example for the Admin user

$baseUrl = 'http://yourhost.abc';
$scriptName = $_SERVER['SCRIPT_NAME'];
$callbackUrl = 'http://scripthost.xyz' . $scriptName;
$temporaryCredentialsRequestUrl = $baseUrl."/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = $baseUrl.'/admin/oauth_authorize';
$customerAuthorizationUrl = $baseUrl.'/oauth/authorize';
$accessTokenRequestUrl = $baseUrl.'/oauth/token';
$apiUrl = $baseUrl.'/api/rest';
$consumerKey = 'Your API consumer key';
$consumerSecret = 'Your API consumer key';

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: ' . $customerAuthorizationUrl . '?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/products";
        $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json'));
        $productsList = json_decode(json_encode($oauthClient->getLastResponse()), FALSE);

        echo $productsList;
    }
} catch (OAuthException $e) {
    print_r($e->getMessage());
    echo "&lt;br/&gt;";
    print_r($e->lastResponse);
}

?>
/**此外,回调URL与PHP调用脚本相同。换句话说,您必须重定向到此脚本。此外,回调脚本和服务器都应该位于live server或本地服务器上*/


/**此外,回调URL与PHP调用脚本相同。换句话说,您必须重定向到此脚本。此外,回调脚本和服务器都应该位于live server或本地服务器上*/

1-您可以使用$adminAuthorizationUrl作为管理员用户使用Magento 1.x REST API访问Magento商店资源。2-您可以使用$customerAuthorizationUrl作为客户用户使用Magento 1.x REST API访问Magento店铺资源。1-您可以使用$adminAuthorizationUrl作为管理员用户使用Magento 1.x REST API访问Magento店铺资源。2-您可以使用$customerAuthorizationUrl作为客户用户使用Magento 1.x REST API访问Magento商店资源。
/**
 * Example of retrieving the products list using Admin account via Magento REST API. OAuth authorization is used
 * Preconditions:
 * 1. Install php oauth extension
 * 2. If you were authorized as a Customer before this step, clear browser cookies for 'yourhost'
 * 3. Create at least one product in Magento
 * 4. Configure resource permissions for Admin REST user for retrieving all product data for Admin
 * 5. Create a Consumer
 */
// $callbackUrl is a path to your file with OAuth authentication example for the Admin user

$baseUrl = 'http://yourhost.abc';
$scriptName = $_SERVER['SCRIPT_NAME'];
$callbackUrl = 'http://scripthost.xyz' . $scriptName;
$temporaryCredentialsRequestUrl = $baseUrl."/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = $baseUrl.'/admin/oauth_authorize';
$customerAuthorizationUrl = $baseUrl.'/oauth/authorize';
$accessTokenRequestUrl = $baseUrl.'/oauth/token';
$apiUrl = $baseUrl.'/api/rest';
$consumerKey = 'Your API consumer key';
$consumerSecret = 'Your API consumer key';

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: ' . $customerAuthorizationUrl . '?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/products";
        $oauthClient->fetch($resourceUrl, array(), 'GET', array('Content-Type' => 'application/json'));
        $productsList = json_decode(json_encode($oauthClient->getLastResponse()), FALSE);

        echo $productsList;
    }
} catch (OAuthException $e) {
    print_r($e->getMessage());
    echo "&lt;br/&gt;";
    print_r($e->lastResponse);
}

?>