使用变量API调用另一个API Shopee开放平台

时间:2018-07-24 04:05:08

标签: php json api

早上,我在致电shopee.item.Add时遇到问题。我必须使用另一个api来调用它,但是我仍然对代码的错误感到困惑。我已经先调用了另一个api,但我不知道问题出在哪里。我总是得到错误参数。

Shopee API Add Item

function addItem(){
// POST /projects.json with HMAC authentication

// Parameters
$tpm_base_url4 = 'https://partner.shopeemobile.com/api/v1/item/add'; // ending with /
$private_key4  = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

// Body (json encoded array)
$request_body4 = json_encode(array(
	'category_id' => 7276,
	'name'=> 'Minion',
	'description' => 'minion minion minion',
	'images' => array(
	),
	'logistics' => array(
	'logistic_id' => '80003',
	'enabled' => true
	),	 
    'partner_id' => 840380,
    'shopid' => 61643482,
    'timestamp' => time())); // root project
// Calculate the HMAC ($hash)
$unhashed4 = $tpm_base_url4 . '|' . $request_body4;
$hash4     = hash_hmac('sha256', $unhashed4, $private_key4);

// Request headers
$headers4 = array(
    'Authorization: ' . $hash4,
    'Content-Type: application/json; charset=utf-8'
);

// Request
$ch4 = curl_init($tpm_base_url4);
curl_setopt($ch4, CURLOPT_HTTPHEADER, $headers4);
curl_setopt($ch4, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch4, CURLOPT_HEADER, TRUE); // Includes the header in the output
curl_setopt($ch4, CURLOPT_POST, TRUE);
curl_setopt($ch4, CURLOPT_POSTFIELDS, $request_body4);
$result4 = curl_exec($ch4);
$status4 = curl_getinfo($ch4, CURLINFO_HTTP_CODE);
curl_close($ch4);

// Get headers and body
list($headers4, $body4) = explode("\r\n\r\n", $result4, 2);
$arr_headers4 = explode("\r\n", $headers4);
$arr_body4    = json_decode($body4, TRUE);

// Show status and the new project id
echo 'Status: ' . $status4 . '<br/>';
print_r($arr_body4);
echo '<br/>';}

getCategories();
getAttributes();
getLogistics();
addItem();

0 个答案:

没有答案