0

这是在命令行中工作的 curl 代码:

  $ curl -F file=@/path/to/index.html -u lslkdfmkls@gmail.com -F 'data={"title":"API V1  App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}' https://build.phonegap.com/api/v1/apps

这是我的代码:

$ch = curl_init();

$data = array("title"=>"sampele title","package"=>"com.fsdlfn.sdfknsdj","version"=>"0.1.0","create_method"=>"file","file"=>"@/path/myfolder/myfile.zip");

$jsdata = json_encode($data);

curl_setopt($ch, CURLOPT_URL, 'https://build.phonegap.com/api/v1/app?auth_token='.$token); //got this token already, so using that here .
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsdata);

echo curl_exec($ch);

curl 命令在终端中工作,但上面的 curl 命令是用 php 编写的,返回错误:

{"error":"no create_method specified: file, remote_repo, or hosted_repo"}

有什么解决办法?

谢谢

4

1 回答 1

0

试试这个:

$ch = curl_init();

$data = array("title"=>"sampele title","package"=>"com.fsdlfn.sdfknsdj","version"=>"0.1.0","create_method"=>"file");

$jsdata = json_encode($data);

curl_setopt($ch, CURLOPT_URL, 'https://build.phonegap.com/api/v1/app?auth_token='.$token); //got this token already, so using that here .
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => $jsdata, 'file'=>'@/path/myfolder/myfile.zip'));

echo curl_exec($ch);

这就是我进行修复的原因:

  1. $datajson_encoded,所以 cURL 不会看到该file属性,它将作为字符串发送,而不是读取文件 - 这就是为什么我file直接放入POSTFIELDS
  2. 该变量$data不会导致它作为data=...单个字符串发送(如POSTing XML)。
于 2012-06-28T22:39:33.700 回答