我想使用Delphi2010和Indy10 (IdHttp组件)完成下一个curl请求(php),但我不确定是否可以完成。我可以使用IdHttp组件获取、放置和发布(CRUD),但在本例中,我不知道如何实现它。我不感兴趣如何使用Delphi模拟curl命令,而是如何实现这个特定的帖子来上传文件。
$url = 'http://example.com';
$key = 'YOUR_GENERATED_API_ACCESS_KEY';
$psProductId = 19;
$urlImage = $url.'/api/images/products/'.$psProductId.'/';
//Here you set the path to the image you need to upload
$image_path = '/path/to/the/image.jpg';
$image_mime = 'image/jpg';
$args['image'] = new CurlFile($image_path, $image_mime);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_URL, $urlImage);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200 == $httpCode) {
echo 'Product image was successfully created.';
}发布于 2020-04-13 20:50:33
通常有两种方式可以上传带有HTTP的文件:
PUT请求,multipart/form-data媒体类型的POST请求,其中HTTP是一系列MIME部件,其中文件数据位于一个部分,通常在其他部分中有额外的字段来描述该文件,或者提供额外的输入。要用Indy发送multipart/form-data编码的POST请求,您需要使用以TIdMultipartFormDataStream对象作为输入的TIdHTTP.Post()方法的重载版本。然后,您可以根据需要将文件(和其他字段)添加到TIdMultipartFormDataStream中。
https://stackoverflow.com/questions/61195880
复制相似问题