Я работаю с программой, в которой нам нужно получить данные из API тарифов FedEx и Transit, API отлично работает с номером счета, который связан с проектом API на портале разработчиков, но как только я пытаюсь изменить другой номер счета, API возвращает ошибка:
file_get_contents(https://apis.fedex.com/rate/v1/rates/quotes): Не удалось
открыть поток: HTTP-запрос не выполнен! HTTP/1.1 400 Bad Request
Не могу найти достаточно информации в документации API и в поиске.
Вопрос 1. Я хочу подтвердить, существует ли какое-либо ограничение на API, согласно которому мы можем получать данные только из тех учетных записей, которые связаны? или нам нужно изменить наш параметр?
Вопрос 2. Я использовал блок try catch для обработки ошибок, но эти ошибки все равно приводят к сбою приложения. Есть ли способ справиться с этим?
public function callAPI()
{
$this->accountNumber->set_value('11111111');
//set shipper postalcode and country code
$this->shipper->set_postalCode(12345);
$this->shipper->set_countryCode('US');
$this->shipper->set_residential(false);
//set recipient postalcode and country code
$this->recipient->set_postalCode(456789);
$this->recipient->set_countryCode('US');
$this->recipient->set_residential(true);
//set weight and units
$this->weight->set_units('LB');
$this->weight->set_value(26);
$this->requestedShipment->set_shipper(['address' => $this->shipper]);
$this->requestedShipment->set_recipient(['address' => $this->recipient]);
$this->requestedShipment->set_requestedPackageLineItems(array(['weight' => $this->weight]));
//set pickup type
//types: "CONTACT_FEDEX_TO_SCHEDULE", "DROPOFF_AT_FEDEX_LOCATION", "USE_SCHEDULED_PICKUP"
$this->requestedShipment->set_pickupType("USE_SCHEDULED_PICKUP");
// set type of rate to be returned
//types: "LIST", "INCENTIVE", "ACCOUNT", "PREFERRED" (see documentation for explanations)
$this->requestedShipment->set_rateRequestType(['ACCOUNT']);
//set shipment date
$this->requestedShipment->set_shipDateStamp("2023-08-17");
$this->rateRequestControlParameters->set_return_tranist(true);
$accountNumberJSON = json_encode($this->accountNumber);
$requestedShipmentJSON = json_encode($this->requestedShipment);
$url = "https://apis.fedex.com/oauth/token";
//client id is api key and client secret is secret key
$data = [
'grant_type' => 'client_credentials',
'client_id' => 'my_clinet_id',
'client_secret' => 'my_clinet_secret'
];
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$resp = file_get_contents($url, false, $context);
$response_data = json_decode($resp);
$access_token = $response_data->access_token;
//calls function if token is working (token changes every minute)
if (isset($access_token)) {
echo $this->get_rates($access_token, $this->accountNumber, $this->requestedShipment, $this->rateRequestControlParameters);
}
}
function get_rates($token, $acct, $ship, $rateReqCtrlPara)
{
$url = "https://apis.fedex.com/rate/v1/rates/quotes";
$data = ['accountNumber' => $acct, 'requestedShipment' => $ship, 'rateRequestControlParameters' => $rateReqCtrlPara, 'carrierCodes' => array('FDXG') ];
$options = array(
'http' => array(
'header' => [
"Content-type: application/json",
"Authorization: Bearer {$token}",
"X-locale: en_US"
],
'method' => 'POST',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
try{
$resp_data = file_get_contents($url, false, $context); //error here
}
catch (exception $e) { echo $e; }
return $resp_data;
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... e-the-acco
API тарифов и транзитного времени выдает ошибку 400 Bad Request, если я меняю номер счета с помощью codeigniter 4. ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Наличие 2 форм в 1 html-файле вызывает ошибку: 400 Bad Request (flask)
Anonymous » » в форуме Python - 0 Ответы
- 17 Просмотры
-
Последнее сообщение Anonymous
-