2012-09-04 29 views
1

我想用的API上https://build.phonegap.com/docs/api從命令行PhoneGap的API捲曲到PHP

給它需要以下curl命令來運行,這將返回一個令牌建立PhoneGap的應用。

$ curl -u [email protected] -X POST "" https://build.phonegap.com/token 

這個捲曲語句在命令行中對我有效。我想在php中使用它。我嘗試了下面給出的PHP代碼,但它不工作。

$username = "[email protected]"; 
$password = "PASSWORD"; 
$target_url = "https://build.phonegap.com/token" 

$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_POSTFIELDS, ''); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 

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

回答

1

您也可以嘗試shell_exec()功能:

$cmd = "curl -u [email protected] -X POST "" https://build.phonegap.com/token"; 
$out = shell_exec($cmd); 
$data = json_decode($out); 
+0

謝謝您的幫助。 –

+0

它爲我工作。 –

0

我找到了解決方案。

我的本地主機設置存在一些問題。當我將代碼上傳到Web服務器時,它開始工作。