【发布时间】:2022-10-24 10:14:25
【问题描述】:
我正在尝试连接到 API,这应该使用 cURL 完成。
这就是文档告诉我要发送的内容(尽管使用我自己的数据,这只是示例)。
curl --request POST \
--url https://api.reepay.com/v1/subscription \
--header 'Accept: application/json' \
-u 'priv_11111111111111111111111111111111:' \
--header 'Content-Type: application/json' \
--data '{"plan":"plan-AAAAA",
"handle": "subscription-101",
"create_customer": {
"handle": "customer-007",
"email": "joe@example.com"
},
"signup_method":"link"}'
我尝试过的是这样,但我得到了错误:
$postdata = array();
$postdata['plan'] = 'plan-AAAAA';
$postdata['handle'] = 'subscription-101';
$postdata['create_customer'] = ["handle" => "customer-007", "email" => "joe@example.com"];
$postdata['signup_method'] = 'link';
$cc = curl_init();
curl_setopt($cc,CURLOPT_POST,1);
curl_setopt($cc,CURLOPT_RETURNTRANSFER,1);
curl_setopt($cc,CURLOPT_URL, "https://api.reepay.com/v1/subscription");
curl_setopt($cc,CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($cc);
echo $result;
这是我得到的错误:{"error":"不支持的媒体类型","path":"/v1/subscription","timestamp":"2022-10-22T11:42:11.733+00:00","http_status":415,"http_reason ":"不支持的媒体类型"}
谁能帮我提出正确的要求?
【问题讨论】:
-
该示例说,
application/json被接受,但您发布的是application/x-www-form-urlencoded。您需要对 postdata 进行 json_encode 并将其放入正文 + 设置适当的内容类型 -
如果您不介意,我会将其作为答案发布,因此您可以接受;)
-
@HonkderHase 当然:-)