2013-08-28 180 views
4

我試圖通過Magento REST API獲取產品信息;但結果錯誤開始「[message:protected] =>無效的認證/錯誤的請求(得到500,預期的HTTP/1.1 20X或重定向) ... ................ ....「 不知道爲什麼會發生這種情況;任何人都可以幫助我嗎? 我的代碼如下:Magento REST API錯誤500

$callbackUrl = "http://myhostcom/current_script.php"; 
$temporaryCredentialsRequestUrl = "http://myhostcom/oauth/initiate?oauth_callback=" .    urlencode($callbackUrl); 
$adminAuthorizationUrl = 'http://myhostcom/oauth/authorize'; 
$accessTokenRequestUrl = 'http://myhostcom/oauth/token'; 
$apiUrl = 'http://myhostcom/api/rest'; 
$consumerKey = 'xxxxxx'; 
$consumerSecret = 'xxxxxxxxxxx'; 
echo '<pre/>'; 
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(); 
    $oauthClient->disableSSLChecks(); 


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/465"; 
    $oauthClient->fetch($resourceUrl); 
    $productsList = json_decode($oauthClient->getLastResponse()); 
    print_r($productsList); 
} 
} catch (OAuthException $e) { 
    print_r($e); 
} 

謝謝。

回答

5

試試下面的代碼及其工作

$callbackUrl = "http://myhostcom/current_script.php"; 
$temporaryCredentialsRequestUrl = "http://myhostcom/oauth/initiate?oauth_callback=" .    urlencode($callbackUrl); 
$adminAuthorizationUrl = 'http://myhostcom/oauth/authorize'; 
$accessTokenRequestUrl = 'http://myhostcom/oauth/token'; 
$apiUrl = 'http://myhostcom/api/rest'; 
$consumerKey = 'xxxxxx'; 
$consumerSecret = 'xxxxxxxxxxx'; 
echo '<pre/>'; 
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(); 
    $oauthClient->disableSSLChecks(); 


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/465"; 
    $oauthClient->fetch($resourceUrl, array(), 'GET', array("Content-Type" => "application/json","Accept" => "*/*")); 
    //$oauthClient->fetch($resourceUrl); 
    $productsList = json_decode($oauthClient->getLastResponse()); 
    print_r($productsList); 
} 
} catch (OAuthException $e) { 
    print_r($e); 
} 

希望它可以幫助您

+1

加入'」接受「=>」*/*「'到fetch命令爲我做了詭計,謝謝! – igrossiter

0
$callbackUrl = "http://myhostcom/current_script.php"; 
$temporaryCredentialsRequestUrl = "http://myhostcom/oauth/initiate?oauth_callback=" .    urlencode($callbackUrl); 
$adminAuthorizationUrl = 'http://myhostcom/oauth/authorize'; 
$accessTokenRequestUrl = 'http://myhostcom/oauth/token'; 
$apiUrl = 'http://myhostcom/api/rest'; 
$consumerKey = 'xxxxxx'; 
$consumerSecret = 'xxxxxxxxxxx'; 

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', 'Accept' => '*/*')); 
    $productsList = json_decode($oauthClient->getLastResponse()); 
    print_r($productsList); 

    echo "<br/>".$_SESSION['secret']."<br/>"; 

    echo "<br/>".$_SESSION['token']."<br/>"; 
} 
} catch (OAuthException $e) { 
print_r($e->getMessage()); 
echo "<br/>"; 
print_r($e->lastResponse); 
} 
-1

具體是什麼做的是加入了接受頭抓取行:

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