我有以下示例cURL请求,我正在尝试通过PHP运行。
curl --request PUT \
--url 'https://example.com/' \
--user 'APIKEY:ACCESSTOKEN' \
--header 'content-type: application/json' \
--data '{"TableId": "MyHats","Columns": ["Id","Name"],"Data": [["1","Hat 1"],["2","Hat 2"],["3","Hat 3"]]}' \
起初我尝试将旧的POST请求转换为使用PUT并发送JSON,但它似乎没有用。
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$access_token");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
我也尝试过从我在网上找到的一个例子转换的流动PUT请求
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$access_token");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
API没有响应,但它确实返回302状态代码,我告诉它是预期的结果,但是他们没有收到任何数据。