来自shell的curl请求

时间:2013-01-16 08:55:49

标签: php curl

我有简单的PHP脚本。

$url = 'http://test.com/api/images/products/33';
$image_path = '/srv/images/some.jpg';
$key = 'qwerty';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_PUT, true); // Un-commet to edit an image
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '@'.$image_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

该脚本运行良好。我想将此代码转换为简单的curl shell命令。

我试过

curl -v -X POST -d image=@/srv/images/some.jpg \
 http://qwerty@test.com/api/images/products/33

但发生了错误。有什么问题?

1 个答案:

答案 0 :(得分:0)

您应该使用-F(代表multipart/form-data)代替-d(使用application/x-www-form-urlencoded):

所以这应该有效:

    curl -v -X POST -F image=@/srv/images/some.jpg http://qwerty@test.com/api/images/products/33