提问人:Abdul Qadeer 提问时间:10/10/2023 最后编辑:H HAbdul Qadeer 更新时间:10/10/2023 访问量:44
无法使用 curl PHP 将文件发布到 rest API
Unable to post file using curl PHP to rest API
问:
我目前在尝试使用 cURL 上传文件时遇到问题。不幸的是,尽管生成了正确的 JWT 令牌,但我还是收到了响应错误。我已经仔细检查了我的令牌和端点,但我仍然无法识别导致此错误的错误。401 Unauthorized
我将非常感谢您在这个问题上的指导和专业知识。如果您能抽出一些时间来查看我的代码或提供与 401 错误相关的常见问题的见解,那将非常有帮助。
$endpoint = 'https://api.example.com' . $endpoint;
$boundary = uniqid();
$headers = array(
"x-api-key" => $apiKey,
"Authorization"=> "Bearer " . $jwt,
"Content-Type"=> "multipart/form-data; boundary=" . $boundary,
);
// $csv_file = new \CURLFile($filePath, 'text/plain');
$post_data = array(
'Files' => '@' . $filePath . ';type=text/plain',
'Comment' => 'text commment'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = curl_exec($curl);
$info = curl_getinfo($curl);
echo "code: ${info['http_code']}";
print_r($info);
var_dump($response);
$err = curl_error($curl);
echo "error";
var_dump($err);
curl_close($curl);
答: 暂无答案
评论