我正在使用php中的oauth1.0进行第三方api集成。 所有获取方法都有效,但只有后方法api不起作用,无法进行oauth验证。
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://samplewebsite.com/generatebill",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n\"availableTripId\": \"2000002130750088374\",\r\n\"boardingPointId\": \"220692\",\r\n\"droppingPointId\": \"208070\",\r\n\"destination\": \"3\",\r\n\"inventoryItems\": [{\r\n\"fare\": \"105.00\",\r\n\"ladiesSeat\": \"false\",\r\n\"passenger\": {\r\n\"address\": \"S.K.C ROAD\",\r\n\"age\": \"34\",\r\n\"email\": \"univerttt@gmail.com\",\r\n\"gender\": \"MALE\",\r\n\"mobile\": \"9933336069\",\r\n\"name\": \"PRAKASH RAO \",\r\n\"primary\": \"true\",\r\n\"title\": \"Mr\"\r\n},\r\n\"seatName\": \"2\"\r\n}],\r\n\"source\": \"102\"\r\n}",
CURLOPT_HTTPHEADER => array(
"Authorization: OAuth oauth_signature_method='HMAC-SHA1', oauth_signature='6NzsraQZ0GcfEKxcOXYP4fRqZxQ%3D', oauth_nonce='5cf2603501283', '
. 'oauth_timestamp='1559388213', oauth_consumer_key='xxxxxxxxxx', oauth_version='1.0','Content-Type: application/json'"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
如果让我知道oauth1.0有任何好的库,我的代码有什么问题
答案 0 :(得分:0)
请尝试使用json_encode()
函数,而不要在CURLOPT_POSTFIELDS
参数中手动添加JSON字符串。有时手动添加字符串,我们会错过逗号,引号或双引号匹配。
$arrParams = [
'availableTripId' => '2000002130750088374',
'boardingPointId' => '220692',
'droppingPointId' => '208070',
...
];
$postParams = json_encode($arrParams);
curl_setopt_array($curl, array(
...
CURLOPT_POSTFIELDS => $postParams,
...
));
希望对您有帮助!