请帮助我理解 API 的文档。 我需要一个 PHP 示例,说明如何将 CURL 用于此类文档描述以准备正确的 POST 请求。 拜托!
POST /api_service/PublicAPI/createRegistry HTTP/1.1
Host: test-api.api_service:8742
Content-Type: application/xml
Authorization: Basic (as a partner)
<api_service:createRegistry xmlns:api_service="http://api_service">
<api_service:registryID>?</api_service:registryID>
<api_service:orders>
<api_service:order>
<api_service:orderID>?</api_service:orderID>
<api_service:info>
<api_service:accountNumber>?</api_service:accountNumber>
</api_service:shipperInfo>
</api_service:order>
</api_service:orders>
</api_service:createRegistryShipment>
这是我的尝试,但我总是收到错误:400 Bad Request。普通的 HTTP 请求已发送到 HTTPS 端口
$fields = array(
'createRegistry' => array(
'registryID' => '05/03/2023 17:29:05',
'orders' => array(
'order' => array(
'orderID' => '12345678',
'info' => array(
'accountNumber' => '12345678'
)
)
)
)
);
$fields_string = http_build_query($fields);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8742",
CURLOPT_URL => "http://test-api.api_service:8742/npi/PublicAPI/createRegistry ",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_TIMEOUT => 30,
CURLOPT_POSTFIELDS => $fields_string,
CURLOPT_HTTPHEADER => array(
'POST: /npi/PublicAPI/createRegistryShipment HTTP/1.1',
'Host: test-api.api_service:8742',
'Content-Type: application/xml',
'Authorization:' . base64_encode('Username:Password')
)
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
var_dump($response);