Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用RESTAPI Magento的问题_Php_Magento_Oauth - Fatal编程技术网

Php 使用RESTAPI Magento的问题

Php 使用RESTAPI Magento的问题,php,magento,oauth,Php,Magento,Oauth,我一直在努力通过RESTAPI连接到我的Magento商店。 我已尝试作为独立程序从XAMPP进行连接:以下是代码: <?php $callbackUrl = "http://healthandmed.com/oauth_admin.php"; $temporaryCredentialsRequestUrl = "http://healthandmed.com/oauth/initiate?oauth_callback=" . urlencode($callbackUrl); $admin

我一直在努力通过RESTAPI连接到我的Magento商店。 我已尝试作为独立程序从XAMPP进行连接:以下是代码:

<?php
$callbackUrl = "http://healthandmed.com/oauth_admin.php";
$temporaryCredentialsRequestUrl =
"http://healthandmed.com/oauth/initiate?oauth_callback=" .
urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://healthandmed.com/admin123/oauth_authorize';
$accessTokenRequestUrl = 'http://healthandmed.com/oauth/token';
$apiUrl = 'http://healthandmed.com/api/rest';
$consumerKey = 'xxxxxxxxxxx';
$consumerSecret = 'xxxxxxxxxxxxx';

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);
       $productsList = json_decode($oauthClient->getLastResponse());
       echo count($productsList);
   }
} catch (OAuthException $e) {
   print_r($e);
}
    ?>

我还尝试了Inchoo的教程: /消费-magento-rest-zend_oauth_消费者/

两种解决方案将我带到了同一个地方:
显示以下错误消息: “发生错误。您的授权请求无效。”


任何帮助都将不胜感激

我认为问题在于您的管理员授权Url,即,$adminAuthorizationUrl该Url必须是,将“admin123”替换为“Admin”。执行此操作时,系统会提示您登录到magento admin后端,然后会要求您进行授权。

通常,最好将代码放在此处,而不是粘贴到此处。谢谢。我在这里潜伏了很长时间。这是我的第一篇文章。