【发布时间】:2021-06-05 05:25:32
【问题描述】:
我正在尝试创建一个php plugin 来使用API 并在发送post 请求时接收响应。
对于 API ,标头必须设置为 Content-Type: application/x-www-form-urlencoded 并且请求对象将作为 request :{Json data} 发送。我已经在 Postman 中对其进行了测试,并且收到了响应,但是在尝试使用 php 进行集成时,出现以下错误 Missing parameter: request.
我正在分享代码以及 Postman 的屏幕截图。请帮忙
<?php
$service_url = 'https://********/PostRequest';
$curl = curl_init($service_url);
//collection Object
$data = array(
'request' => array(
"Key1" => "*****",
"key2" => "",
"key3" => "****",
"key4" => "****",
"key5" => "****",
"key6" => "****",
"key7" => "***",
"key8" => "****",
"key9" => "***",
"key10" => "****",
"key11" => "***",
"ItemList" => array(
"subkey1" => "***",
"subkey2" => "***",
"subkey3" => "****",
"subkey4" => "****",
"subkey5" => "****",
"subkey6" => "*****",
"subkey7" => "***",
"subkey8" => "***",
"subkey9" => "***",
"subkey10" => "**",
"Subkey11" => "**",
"subkey12" => "**",
"subkey13" => "04",
"subkey14" => "010101",
"Subkey15" => "R",
"Subkey16" => "70",
"Subkey17" => array(
"string" => "00"
)
),
"Address" => array(
"PrimaryAddressLine" => "",
"SecondaryAddressLine" => "",
"County" => "",
"City" => "",
"State" => "",
"PostalCode" => "****",
"Plus4" => "",
"Country" => "",
"Geocode" => "",
"VerifyAddress" => "false"
),
)
);
$data_json = json_encode($data);
// Set the CURLOPT_RETURNTRANSFER option to true
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the CURLOPT_POST option to true for POST request
curl_setopt($curl, CURLOPT_POST, true);
// Set custom headers for RapidAPI Auth and Content-Type header
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
$response = curl_exec($curl);
curl_close($curl);
print_r($response);
die();
Postman 截图:-
【问题讨论】:
-
URL 编码与 JSON 不同。