简体   繁体   English

cURL到PHP中的JSON-POST无法正常工作

[英]cURL to JSON in PHP - POST not working

I have the following cURL command, to add an image to my server, which works just fine: 我有以下cURL命令,可将图像添加到我的服务器,效果很好:

curl -i -X POST -u user:pw -F 'post[image]'=@image.jpg http://domain.com/post.json

Now I want to make this work in PHP. 现在,我想用PHP进行这项工作。 I seem to get everything working, except for the 'post[image]'=@image.jpg part. 除了“ post [image]” = @ image.jpg部分,我似乎一切正常。

What do I need to pass to my curl_setopt($ch, CURLOPT_POSTFIELDS) field make this work? 要传递给curl_setopt($ch, CURLOPT_POSTFIELDS)字段curl_setopt($ch, CURLOPT_POSTFIELDS)我需要什么? Can someone help me out here? 有人可以帮我吗?

$url = 'host/script.php';
$post = array('image' => '@image.jpg');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec($ch);
curl_close($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM