我想通过PHP发布api的数组。我已经为此使用了oAuth 1.0,但如何使用oAuth POST JSON主体数组。
<? php
//code start
try
{
$postData = array(
'ProductID' => $productID,
'InventoryType' => $InventoryType,
'ProductCostPrice' => $productCost
);
$postData = json_encode($postData);
$oauth = new OAuth($this->consumer_key, $this->consumer_secret);
$oauth->enableDebug(); // simple debug flag for checking error messages
$oauth->disableSSLChecks(); // avoids an SSL issue when testing
$oauth->setToken($this->access_token, $this->token_secret);
## POST JSON body array ##
$oauth->setBody('body',$postData);
##
$url = $this->product_update_url.$productID;
$oauth->fetch($url, OAUTH_AUTH_TYPE_FORM);
$response_info = $oauth->getLastResponseInfo();
header("Content-Type: {$response_info["content_type"]}");
$dataList = (array) json_decode($oauth->getLastResponse());
echo "<pre>";
print_r($dataList);
echo "</pre>";
exit;
} catch(OAuthException $E) {
Mage::log('error', null, 'error.log');
}
===============
网址:http://php.net/manual/en/class.oauth.php
请你帮忙,我如何使用oAuth POST json主体数组。
在OAuth1规范中规范化请求参数的方式意味着,只有当正文采用表单url编码格式(即param1=value1¶m2=value2
或arr[]=val1&arr[]=val2
)时,才应对其进行签名
这是由于OAuth1计算请求签名的方式,其中包括按名称字母顺序对参数进行排序。
在上面的例子中,如果可能的话,你应该尝试对数据数组进行url编码。
$postData = array(
'ProductID' => $productID,
'InventoryType' => $InventoryType,
'ProductCostPrice' => $productCost
);
// "ProductID=1&InventoryType=widget&ProductCostPrice=3.5"
$body = http_build_query($postData);
...
$oauth->setBody('body', $body);
如果出于任何原因,这都是不可能的,那么在解码请求时,您需要确保在签名计算中没有使用请求体。
如果你正在为API使用HTTPS(你应该这样做),你应该可以处理签名中无法验证的请求主体。如果您允许向API发送HTTP请求,那么您应该有一些机制来确认请求主体没有被更改。
我看到的一种方法是对JSON主体字符串进行散列,然后将该散列作为查询参数包含在请求中(因此它包含在签名计算中)。然后,作为API解码过程的一部分,您将确认JSON主体哈希为签名查询值。
这个问题有点老,但我是通过谷歌搜索找到的,所以我认为最好给未来的搜索者留下答案。