我想使用Delphi 2010和Indy10(IdHttp组件)进行下一个curl请求(php),但是我不确定是否可以完成。我可以使用IdHttp组件执行GET,PUT和POST(CRUD),但是在这种情况下,我不知道如何实现它。我对如何用Delphi模拟curl命令不感兴趣,但对如何实现此特定的POST上传文件不感兴趣。
$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,CURLINFO_HEADER_OUT,CURLOPT_URL,$urlImage);
curl_setopt($ch,CURLOPT_POST,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.';
}