2013-03-21 3 views
0

RESTful API를 사용하여 제품을 만들려고합니다. RESTCLIENT firefox addon을 사용하여이 기능을 달성했지만 스크립트 사용에 실패했습니다. 제품을 나열 할 수는 있지만 스크립트를 사용하여 제품을 만들 수는 없습니다. 액세스 거부 오류가 발생했습니다. 누구든지 나를 도울 수 있습니까?Oauth & curl을 사용하여 제품 만들기

내 스크립트는 다음과 같습니다.

$url = 'http://magento.com/api/rest/products'; 
$method = 'POST'; 

# headers and data (this is API dependent, some uses XML) 
$headers = array(
'Accept: application/json', 
'Content-Type: application/json', 
'oauth_signature_method : HMAC-SHA1', 
'oauth_nonce : ilJuravy9KVYm6R', 
'oauth_timestamp : 1363848967', 
'oauth_consumer_key : xxx', 
'oauth_consumer_secret : yyy', 
'oauth_token : zzz', 
'oauth_token_secret : xyz', 
'oauth_signature : 4admodOkAj2pKwhO5Tk6TEjc7Rg%3D', 
'oauth_verifier: mrr1350pp0j8hiyv31kzxhko97hyyuwx', 
'oauth_version : 1.0', 
); 
$data = json_encode(
array(
    'type_id'   => 'simple', 
    'attribute_set_id' => 4, 
    'sku'    => 'simple' . uniqid(), 
    'weight'   => 1, 
    'status'   => 1, 
    'visibility'  => 4, 
    'name'    => 'Simple Product', 
    'description'  => 'Simple Description', 
    'short_description' => 'Simple Short Description', 
    'price'    => 99.95, 
    'tax_class_id'  => 0, 
) 
); 

$handle = curl_init(); 
curl_setopt($handle, CURLOPT_URL, $url); 
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); 

switch($method) { 
case 'GET': 
break; 
case 'POST': 
curl_setopt($handle, CURLOPT_POST, true); 
curl_setopt($handle, CURLOPT_POSTFIELDS, $data); 
break; 
case 'PUT': 
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT'); 
curl_setopt($handle, CURLOPT_POSTFIELDS, $data); 
break; 
case 'DELETE': 
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE'); 
break; 
} 

echo $response = curl_exec($handle); 
echo $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); 

답변

3

당신은 일을 아래에 언급 셋을 생성해야하고 다른 일들이 oauth_consumer_key 같은 정적,의 oauth_token 등

1.timestmap 2.signature 내가 만물을 생성 한

3.nonce 아래 코드를 참조하십시오.

$nonce = substr(md5(uniqid('nonce_', true)),0,16); 
$temprealm="http://magentohost/api/rest/products"; 
$realm=urlencode($temprealm); 
$oauth_version="1.0"; 
$oauth_signature_method="HMAC-SHA1"; 
$oauth_consumer_key="lro2hnoh3c8luvhcr49j6qgygmyvw7e3"; 
$oauth_access_token="xbqe4wnu3zv357gimpdnuejvcbtk51ni"; 
$oauth_method="GET"; 
$oauth_timestamp=time(); 
$algo="sha1"; 
$key="sb88hfdihyg25ipt1by559yzbj2m3861&s7uhaheu8nrx961oxg6uc3os4zgyc2tm"; //consumer secret & token secret //Both are used in generate signature 
$data="oauth_consumer_key=".$oauth_consumer_key."&oauth_nonce=".$nonce."&oauth_signature_method=".$oauth_signature_method."&oauth_timestamp=".$oauth_timestamp."&oauth_token=".$oauth_access_token."&oauth_version=".$oauth_version; 

$send_data=$oauth_method."&".$realm."&".urlencode($data); 
$sign=hash_hmac($algo,$send_data,$key,1); // consumer key and token secrat used here 
$fin_sign=base64_encode($sign); 
$curl = curl_init(); 



curl_setopt($curl,CURLOPT_HTTPHEADER,array('Authorization : OAuth realm='.$realm.', oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_nonce="'.$nonce.'", oauth_timestamp="'.$oauth_timestamp.'", oauth_consumer_key='.$oauth_consumer_key.', oauth_token='.$oauth_access_token.', oauth_signature="'.$fin_sign.'"')); 

curl_setopt ($curl, CURLOPT_URL,$temprealm); 
$xml=curl_exec($curl); 
관련 문제