【发布时间】:2020-04-13 20:06:19
【问题描述】:
我想使用 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, 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.';
}
【问题讨论】:
标签: curl delphi-2010 indy10