2012-09-04 23 views
1

我正在使用PhoneGap Build API上傳文件並創建其apk。你可以在這裏查看https://build.phonegap.com/docs/write_apiPhoneGap Build API:CURL命令行到PHP

我在使用php實現curl時遇到問題。下面的命令行上的curl語句正常工作。

$ curl -F [email protected]/Users/alunny/index.html -u [email protected] -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:文件,remote_repo,或hosted_repo」

請幫我解決這個問題。

謝謝

+1

可能重複? http://stackoverflow.com/questions/11253848/curl-not-sending-post-data-but-works-properly-in-terminal – Mike

+0

謝謝邁克的幫助。我使用您給我的副本獲得解決方案。 –

回答

1

嘗試了shell_exec()函數:

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