PhoneGap Build API:PHP的CURL命令行

时间:2012-09-04 22:29:53

标签: php cordova file-upload curl syntax-error

我正在使用PhoneGap Build API上传文件并创建其apk。您可以在此处查看https://build.phonegap.com/docs/write_api

我在使用php实现curl时遇到问题。命令行上的以下curl语句正常工作。

$ curl -F file=@/Users/alunny/index.html -u andrew.lunny@nitobi.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

当我尝试使用php编写此代码时出现错误。我写了以下代码。

$username = "email";
$password = "password";
$target_url = "https://build.phonegap.com/api/v1/apps";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$post = array(
        "file"=>"@www.zip",
        "data"=>json_encode(array("title"=>"My Test App","package"=>"com.alunny.apiv1","version"=>"0.1.0","create_method"=>"file"))
    );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$result = curl_exec($ch);
curl_close ($ch);

通过运行此代码,我收到此错误“没有指定create_method:file,remote_repo或hosted_repo”

请帮我解决这个问题。

谢谢

1 个答案:

答案 0 :(得分:1)

尝试shell_exec()函数:

$cmd = "https://build.phonegap.com/api/v1/apps";
$out = shell_exec($cmd);
$data = json_decode($out);